1616from mcp .client .sse import sse_client
1717from mcp .client .streamable_http import GetSessionIdCallback , streamablehttp_client
1818from mcp .shared .message import SessionMessage
19- from mcp .types import CallToolResult , GetPromptResult , InitializeResult , ListPromptsResult , ListResourcesResult , ReadResourceResult
19+ from mcp .types import (
20+ CallToolResult ,
21+ GetPromptResult ,
22+ InitializeResult ,
23+ ListPromptsResult ,
24+ ListResourcesResult ,
25+ ReadResourceResult ,
26+ )
2027from typing_extensions import NotRequired , TypedDict
2128
2229from ..exceptions import UserError
@@ -80,15 +87,17 @@ async def call_tool(self, tool_name: str, arguments: dict[str, Any] | None) -> C
8087 pass
8188
8289 @abc .abstractmethod
83- async def list_resources (self )-> ListResourcesResult :
90+ async def list_resources (self ) -> ListResourcesResult :
8491 """List resources available on the server."""
8592 pass
93+
8694 @abc .abstractmethod
87- async def read_resource (self , uri :str )-> ReadResourceResult :
95+ async def read_resource (self , uri : str ) -> ReadResourceResult :
8896 """Read the content of a resource by URI."""
8997 pass
98+
9099 @abc .abstractmethod
91- async def subscribe_resource (self , uri :str , ** kwargs ) -> AsyncIterator [SessionMessage ]:
100+ async def subscribe_resource (self , uri : str , ** kwargs ) -> AsyncIterator [SessionMessage ]:
92101 """Subscribe to resource updates."""
93102 pass
94103
@@ -105,7 +114,7 @@ async def get_prompt(
105114 ) -> GetPromptResult :
106115 """Get a specific prompt from the server."""
107116 pass
108-
117+
109118
110119class _MCPServerWithClientSession (MCPServer , abc .ABC ):
111120 """Base class for MCP servers that use a `ClientSession` to communicate with the server."""
@@ -301,10 +310,6 @@ async def connect(self):
301310 await self .cleanup ()
302311 raise
303312
304-
305-
306-
307-
308313 async def list_tools (
309314 self ,
310315 run_context : RunContextWrapper [Any ] | None = None ,
@@ -343,29 +348,27 @@ async def call_tool(self, tool_name: str, arguments: dict[str, Any] | None) -> C
343348
344349 return await self ._run_with_retries (lambda : session .call_tool (tool_name , arguments ))
345350
346- async def list_resources (self )-> ListResourcesResult :
351+ async def list_resources (self ) -> ListResourcesResult :
347352 if not self .session :
348353 raise UserError ("Server not initialized. Make sure you call `connect()` first." )
349354 session = self .session
350355 assert session is not None
351356 return await session .list_resources ()
352-
353- async def read_resource (self ,uri :str )-> ReadResourceResult :
357+
358+ async def read_resource (self , uri : str ) -> ReadResourceResult :
354359 if not self .session :
355360 raise UserError ("Server not initialized. Make sure you call `connect()` first." )
356361 session = self .session
357362 assert session is not None
358363 return await session .read_resource (uri )
359-
360- async def subscribe_resource (self , uri :str , ** kwargs )-> AsyncIterator [SessionMessage ]:
364+
365+ async def subscribe_resource (self , uri : str , ** kwargs ) -> AsyncIterator [SessionMessage ]:
361366 if not self .session :
362367 raise UserError ("Server not initialized. Make sure you call `connect()` first." )
363368 session = self .session
364369 assert session is not None
365370 async for msg in session .subscribe_resource (uri , ** kwargs ):
366371 yield msg
367-
368-
369372
370373 async def list_prompts (
371374 self ,
@@ -634,8 +637,6 @@ class MCPServerStreamableHttpParams(TypedDict):
634637 """Custom HTTP client factory for configuring httpx.AsyncClient behavior."""
635638
636639
637-
638-
639640class MCPServerStreamableHttp (_MCPServerWithClientSession ):
640641 """MCP server implementation that uses the Streamable HTTP transport. See the [spec]
641642 (https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#streamable-http)
@@ -731,4 +732,4 @@ def create_streams(
731732 @property
732733 def name (self ) -> str :
733734 """A readable name for the server."""
734- return self ._name
735+ return self ._name
0 commit comments