Skip to content

Commit c7d8f11

Browse files
committed
Formatting
1 parent a891ad4 commit c7d8f11

File tree

8 files changed

+87
-58
lines changed

8 files changed

+87
-58
lines changed

mcp_python/client/session.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import timedelta
22

33
from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream
4-
from pydantic import AnyUrl, FileUrl
4+
from pydantic import AnyUrl
55

66
from mcp_python.shared.session import BaseSession
77
from mcp_python.shared.version import SUPPORTED_PROTOCOL_VERSIONS
@@ -21,7 +21,6 @@
2121
JSONRPCMessage,
2222
ListPromptsResult,
2323
ListResourcesResult,
24-
ListRootsResult,
2524
ListToolsResult,
2625
LoggingLevel,
2726
PromptReference,
@@ -71,9 +70,11 @@ async def initialize(self) -> InitializeResult:
7170
sampling=None,
7271
experimental=None,
7372
roots={
74-
# TODO: Should this be based on whether we _will_ send notifications, or only whether they're supported?
73+
# TODO: Should this be based on whether we
74+
# _will_ send notifications, or only whether
75+
# they're supported?
7576
"listChanged": True
76-
}
77+
},
7778
),
7879
clientInfo=Implementation(name="mcp_python", version="0.1.0"),
7980
),
@@ -246,7 +247,9 @@ async def list_prompts(self) -> ListPromptsResult:
246247
ListPromptsResult,
247248
)
248249

249-
async def get_prompt(self, name: str, arguments: dict[str, str] | None = None) -> GetPromptResult:
250+
async def get_prompt(
251+
self, name: str, arguments: dict[str, str] | None = None
252+
) -> GetPromptResult:
250253
"""Send a prompts/get request."""
251254
from mcp_python.types import GetPromptRequest, GetPromptRequestParams
252255

@@ -260,9 +263,15 @@ async def get_prompt(self, name: str, arguments: dict[str, str] | None = None) -
260263
GetPromptResult,
261264
)
262265

263-
async def complete(self, ref: ResourceReference | PromptReference, argument: dict) -> CompleteResult:
266+
async def complete(
267+
self, ref: ResourceReference | PromptReference, argument: dict
268+
) -> CompleteResult:
264269
"""Send a completion/complete request."""
265-
from mcp_python.types import CompleteRequest, CompleteRequestParams, CompletionArgument
270+
from mcp_python.types import (
271+
CompleteRequest,
272+
CompleteRequestParams,
273+
CompletionArgument,
274+
)
266275

267276
return await self.send_request(
268277
ClientRequest(

mcp_python/server/__init__.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
ClientNotification,
1919
ClientRequest,
2020
CompleteRequest,
21+
EmbeddedResource,
2122
EmptyResult,
2223
ErrorData,
2324
JSONRPCMessage,
@@ -31,6 +32,7 @@
3132
PingRequest,
3233
ProgressNotification,
3334
Prompt,
35+
PromptMessage,
3436
PromptReference,
3537
ReadResourceRequest,
3638
ReadResourceResult,
@@ -40,11 +42,9 @@
4042
ServerResult,
4143
SetLevelRequest,
4244
SubscribeRequest,
45+
TextContent,
4346
Tool,
4447
UnsubscribeRequest,
45-
TextContent,
46-
EmbeddedResource,
47-
PromptMessage,
4848
)
4949

5050
logger = logging.getLogger(__name__)
@@ -147,17 +147,14 @@ async def handler(req: GetPromptRequest):
147147
)
148148
case types.EmbeddedResource() as resource:
149149
content = EmbeddedResource(
150-
type="resource",
151-
resource=resource.resource
150+
type="resource", resource=resource.resource
152151
)
153152
case _:
154153
raise ValueError(
155154
f"Unexpected content type: {type(message.content)}"
156155
)
157156

158-
prompt_message = PromptMessage(
159-
role=message.role, content=content
160-
)
157+
prompt_message = PromptMessage(role=message.role, content=content)
161158
messages.append(prompt_message)
162159

163160
return ServerResult(
@@ -175,9 +172,7 @@ def decorator(func: Callable[[], Awaitable[list[Resource]]]):
175172

176173
async def handler(_: Any):
177174
resources = await func()
178-
return ServerResult(
179-
ListResourcesResult(resources=resources)
180-
)
175+
return ServerResult(ListResourcesResult(resources=resources))
181176

182177
self.request_handlers[ListResourcesRequest] = handler
183178
return func
@@ -222,7 +217,6 @@ async def handler(req: ReadResourceRequest):
222217

223218
return decorator
224219

225-
226220
def set_logging_level(self):
227221
from mcp_python.types import EmptyResult
228222

@@ -282,10 +276,17 @@ async def handler(_: Any):
282276
return decorator
283277

284278
def call_tool(self):
285-
from mcp_python.types import CallToolResult, TextContent, ImageContent, EmbeddedResource
279+
from mcp_python.types import (
280+
CallToolResult,
281+
EmbeddedResource,
282+
ImageContent,
283+
TextContent,
284+
)
286285

287286
def decorator(
288-
func: Callable[..., Awaitable[list[str | types.ImageContent | types.EmbeddedResource]]]
287+
func: Callable[
288+
..., Awaitable[list[str | types.ImageContent | types.EmbeddedResource]]
289+
],
289290
):
290291
logger.debug("Registering handler for CallToolRequest")
291292

@@ -298,28 +299,26 @@ async def handler(req: CallToolRequest):
298299
case str() as text:
299300
content.append(TextContent(type="text", text=text))
300301
case types.ImageContent() as img:
301-
content.append(ImageContent(
302-
type="image",
303-
data=img.data,
304-
mimeType=img.mime_type
305-
))
302+
content.append(
303+
ImageContent(
304+
type="image",
305+
data=img.data,
306+
mimeType=img.mime_type,
307+
)
308+
)
306309
case types.EmbeddedResource() as resource:
307-
content.append(EmbeddedResource(
308-
type="resource",
309-
resource=resource.resource
310-
))
310+
content.append(
311+
EmbeddedResource(
312+
type="resource", resource=resource.resource
313+
)
314+
)
311315

312-
return ServerResult(
313-
CallToolResult(
314-
content=content,
315-
isError=False
316-
)
317-
)
316+
return ServerResult(CallToolResult(content=content, isError=False))
318317
except Exception as e:
319318
return ServerResult(
320319
CallToolResult(
321320
content=[TextContent(type="text", text=str(e))],
322-
isError=True
321+
isError=True,
323322
)
324323
)
325324

mcp_python/server/session.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RequestResponder,
1313
)
1414
from mcp_python.types import (
15-
ListRootsResult, LATEST_PROTOCOL_VERSION,
15+
LATEST_PROTOCOL_VERSION,
1616
ClientNotification,
1717
ClientRequest,
1818
CreateMessageResult,
@@ -23,15 +23,16 @@
2323
InitializeRequest,
2424
InitializeResult,
2525
JSONRPCMessage,
26+
ListRootsResult,
2627
LoggingLevel,
28+
ModelPreferences,
29+
PromptListChangedNotification,
30+
ResourceListChangedNotification,
2731
SamplingMessage,
2832
ServerNotification,
2933
ServerRequest,
3034
ServerResult,
31-
ResourceListChangedNotification,
3235
ToolListChangedNotification,
33-
PromptListChangedNotification,
34-
ModelPreferences,
3536
)
3637

3738

mcp_python/server/types.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
"""
2-
This module provides simpler types to use with the server for managing prompts and tools.
2+
This module provides simpler types to use with the server for managing prompts
3+
and tools.
34
"""
45

56
from dataclasses import dataclass
67
from typing import Literal
78

89
from pydantic import BaseModel
910

10-
from mcp_python.types import Role, ServerCapabilities, TextResourceContents, BlobResourceContents
11+
from mcp_python.types import (
12+
BlobResourceContents,
13+
Role,
14+
ServerCapabilities,
15+
TextResourceContents,
16+
)
1117

1218

1319
@dataclass

mcp_python/shared/memory.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515

1616
MessageStream = tuple[
1717
MemoryObjectReceiveStream[JSONRPCMessage | Exception],
18-
MemoryObjectSendStream[JSONRPCMessage]
18+
MemoryObjectSendStream[JSONRPCMessage],
1919
]
2020

21+
2122
@asynccontextmanager
22-
async def create_client_server_memory_streams() -> AsyncGenerator[
23-
tuple[MessageStream, MessageStream],
24-
None
25-
]:
23+
async def create_client_server_memory_streams() -> (
24+
AsyncGenerator[tuple[MessageStream, MessageStream], None]
25+
):
2626
"""
2727
Creates a pair of bidirectional memory streams for client-server communication.
2828

mcp_python/shared/session.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ async def send_request(
154154

155155
try:
156156
with anyio.fail_after(
157-
None if self._read_timeout_seconds is None
157+
None
158+
if self._read_timeout_seconds is None
158159
else self._read_timeout_seconds.total_seconds()
159160
):
160161
response_or_error = await response_stream_reader.receive()
@@ -168,7 +169,6 @@ async def send_request(
168169
f"{self._read_timeout_seconds} seconds."
169170
),
170171
)
171-
172172
)
173173

174174
if isinstance(response_or_error, JSONRPCError):

mcp_python/types.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,9 @@ class ToolListChangedNotification(Notification):
654654
params: NotificationParams | None = None
655655

656656

657-
LoggingLevel = Literal["debug", "info", "notice", "warning", "error", "critical", "alert", "emergency"]
657+
LoggingLevel = Literal[
658+
"debug", "info", "notice", "warning", "error", "critical", "alert", "emergency"
659+
]
658660

659661

660662
class SetLevelRequestParams(RequestParams):
@@ -708,7 +710,8 @@ class ModelHint(BaseModel):
708710

709711
class ModelPreferences(BaseModel):
710712
"""
711-
The server's preferences for model selection, requested of the client during sampling.
713+
The server's preferences for model selection, requested of the client during
714+
sampling.
712715
713716
Because LLMs can vary along multiple dimensions, choosing the "best" model is
714717
rarely straightforward. Different models excel in different areas—some are
@@ -761,7 +764,10 @@ class CreateMessageRequestParams(RequestParams):
761764

762765
messages: list[SamplingMessage]
763766
modelPreferences: ModelPreferences | None = None
764-
"""The server's preferences for which model to select. The client MAY ignore these preferences."""
767+
"""
768+
The server's preferences for which model to select. The client MAY ignore
769+
these preferences.
770+
"""
765771
systemPrompt: str | None = None
766772
"""An optional system prompt the server wants to use for sampling."""
767773
includeContext: IncludeContext | None = None
@@ -911,9 +917,12 @@ class ListRootsResult(Result):
911917

912918
class RootsListChangedNotification(Notification):
913919
"""
914-
A notification from the client to the server, informing it that the list of roots has changed.
915-
This notification should be sent whenever the client adds, removes, or modifies any root.
916-
The server should then request an updated list of roots using the ListRootsRequest.
920+
A notification from the client to the server, informing it that the list of
921+
roots has changed.
922+
923+
This notification should be sent whenever the client adds, removes, or
924+
modifies any root. The server should then request an updated list of roots
925+
using the ListRootsRequest.
917926
"""
918927

919928
method: Literal["notifications/roots/list_changed"]
@@ -940,7 +949,11 @@ class ClientRequest(
940949
pass
941950

942951

943-
class ClientNotification(RootModel[ProgressNotification | InitializedNotification | RootsListChangedNotification]):
952+
class ClientNotification(
953+
RootModel[
954+
ProgressNotification | InitializedNotification | RootsListChangedNotification
955+
]
956+
):
944957
pass
945958

946959

tests/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
capabilities=ServerCapabilities(),
1212
)
1313

14+
1415
@pytest.fixture
1516
def mcp_server() -> Server:
1617
server = Server(name="test_server")
@@ -21,7 +22,7 @@ async def handle_list_resources():
2122
Resource(
2223
uri=AnyUrl("memory://test"),
2324
name="Test Resource",
24-
description="A test resource"
25+
description="A test resource",
2526
)
2627
]
2728

0 commit comments

Comments
 (0)