@@ -36,62 +36,63 @@ flowchart LR
3636
3737The protocol layer handles message framing, request/response linking, and high-level communication patterns.
3838
39- <Tabs >
40- <Tab title = " TypeScript" >
41-
42- ``` typescript
43- class Protocol <Request , Notification , Result > {
44- // Handle incoming requests
45- setRequestHandler<T >(schema : T , handler : (request : T , extra : RequestHandlerExtra ) => Promise <Result >): void
46-
47- // Handle incoming notifications
48- setNotificationHandler<T >(schema : T , handler : (notification : T ) => Promise <void >): void
49-
50- // Send requests and await responses
51- request<T >(request : Request , schema : T , options ? : RequestOptions ): Promise <T >
52-
53- // Send one-way notifications
54- notification(notification : Notification ): Promise <void >
55- }
56- ```
57-
58- </Tab >
59- <Tab title = " Python" >
60-
61- ``` python
62- class Session (BaseSession[RequestT, NotificationT, ResultT]):
63- async def send_request (
64- self ,
65- request : RequestT,
66- result_type : type[Result]
67- ) -> Result:
68- """ Send request and wait for response. Raises McpError if response contains error."""
69- # Request handling implementation
70-
71- async def send_notification (
72- self ,
73- notification : NotificationT
74- ) -> None :
75- """ Send one-way notification that doesn't expect response."""
76- # Notification handling implementation
77-
78- async def _received_request (
79- self ,
80- responder : RequestResponder[ReceiveRequestT, ResultT]
81- ) -> None :
82- """ Handle incoming request from other side."""
83- # Request handling implementation
84-
85- async def _received_notification (
86- self ,
87- notification : ReceiveNotificationT
88- ) -> None :
89- """ Handle incoming notification from other side."""
90- # Notification handling implementation
91- ```
92-
93- </Tab >
94- </Tabs >
39+ <CodeGroup >
40+
41+ ``` typescript TypeScript
42+ class Protocol <Request , Notification , Result > {
43+ // Handle incoming requests
44+ setRequestHandler<T >(
45+ schema : T ,
46+ handler : (request : T , extra : RequestHandlerExtra ) => Promise <Result >,
47+ ): void ;
48+
49+ // Handle incoming notifications
50+ setNotificationHandler<T >(
51+ schema : T ,
52+ handler : (notification : T ) => Promise <void >,
53+ ): void ;
54+
55+ // Send requests and await responses
56+ request<T >(request : Request , schema : T , options ? : RequestOptions ): Promise <T >;
57+
58+ // Send one-way notifications
59+ notification(notification : Notification ): Promise <void >;
60+ }
61+ ```
62+
63+ ``` python Python
64+ class Session (BaseSession[RequestT, NotificationT, ResultT]):
65+ async def send_request (
66+ self ,
67+ request : RequestT,
68+ result_type : type[Result]
69+ ) -> Result:
70+ """ Send request and wait for response. Raises McpError if response contains error."""
71+ # Request handling implementation
72+
73+ async def send_notification (
74+ self ,
75+ notification : NotificationT
76+ ) -> None :
77+ """ Send one-way notification that doesn't expect response."""
78+ # Notification handling implementation
79+
80+ async def _received_request (
81+ self ,
82+ responder : RequestResponder[ReceiveRequestT, ResultT]
83+ ) -> None :
84+ """ Handle incoming request from other side."""
85+ # Request handling implementation
86+
87+ async def _received_notification (
88+ self ,
89+ notification : ReceiveNotificationT
90+ ) -> None :
91+ """ Handle incoming notification from other side."""
92+ # Notification handling implementation
93+ ```
94+
95+ </CodeGroup >
9596
9697Key classes include:
9798
@@ -216,73 +217,71 @@ Errors are propagated through:
216217
217218Here's a basic example of implementing an MCP server:
218219
219- <Tabs >
220- <Tab title = " TypeScript" >
221-
222- ``` typescript
223- import { Server } from " @modelcontextprotocol/sdk/server/index.js" ;
224- import { StdioServerTransport } from " @modelcontextprotocol/sdk/server/stdio.js" ;
225-
226- const server = new Server ({
227- name: " example-server" ,
228- version: " 1.0.0"
229- }, {
230- capabilities: {
231- resources: {}
232- }
233- });
234-
235- // Handle requests
236- server .setRequestHandler (ListResourcesRequestSchema , async () => {
237- return {
238- resources: [
239- {
240- uri: " example://resource" ,
241- name: " Example Resource"
242- }
243- ]
244- };
245- });
246-
247- // Connect transport
248- const transport = new StdioServerTransport ();
249- await server .connect (transport );
250- ```
251-
252- </Tab >
253- <Tab title = " Python" >
254-
255- ``` python
256- import asyncio
257- import mcp.types as types
258- from mcp.server import Server
259- from mcp.server.stdio import stdio_server
260-
261- app = Server(" example-server" )
262-
263- @app.list_resources ()
264- async def list_resources () -> list[types.Resource]:
265- return [
266- types.Resource(
267- uri = " example://resource" ,
268- name = " Example Resource"
269- )
270- ]
271-
272- async def main ():
273- async with stdio_server() as streams:
274- await app.run(
275- streams[0 ],
276- streams[1 ],
277- app.create_initialization_options()
278- )
279-
280- if __name__ == " __main__" :
281- asyncio.run(main())
282- ```
283-
284- </Tab >
285- </Tabs >
220+ <CodeGroup >
221+
222+ ``` typescript TypeScript
223+ import { Server } from " @modelcontextprotocol/sdk/server/index.js" ;
224+ import { StdioServerTransport } from " @modelcontextprotocol/sdk/server/stdio.js" ;
225+
226+ const server = new Server (
227+ {
228+ name: " example-server" ,
229+ version: " 1.0.0" ,
230+ },
231+ {
232+ capabilities: {
233+ resources: {},
234+ },
235+ },
236+ );
237+
238+ // Handle requests
239+ server .setRequestHandler (ListResourcesRequestSchema , async () => {
240+ return {
241+ resources: [
242+ {
243+ uri: " example://resource" ,
244+ name: " Example Resource" ,
245+ },
246+ ],
247+ };
248+ });
249+
250+ // Connect transport
251+ const transport = new StdioServerTransport ();
252+ await server .connect (transport );
253+ ```
254+
255+ ``` python Python
256+ import asyncio
257+ import mcp.types as types
258+ from mcp.server import Server
259+ from mcp.server.stdio import stdio_server
260+
261+ app = Server(" example-server" )
262+
263+ @app.list_resources ()
264+ async def list_resources () -> list[types.Resource]:
265+ return [
266+ types.Resource(
267+ uri = " example://resource" ,
268+ name = " Example Resource"
269+ )
270+ ]
271+
272+ async def main ():
273+ async with stdio_server() as streams:
274+ await app.run(
275+ streams[0 ],
276+ streams[1 ],
277+ app.create_initialization_options()
278+ )
279+
280+ if __name__ == " __main__" :
281+ asyncio.run(main())
282+ ```
283+
284+ </CodeGroup >
286285
287286## Best practices
288287
0 commit comments