Skip to content

Commit 4ac03d4

Browse files
committed
Update convenience methods on ClientSession and ServerSession
1 parent 1634343 commit 4ac03d4

File tree

2 files changed

+133
-3
lines changed

2 files changed

+133
-3
lines changed

mcp_python/client/session.py

Lines changed: 83 additions & 2 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
4+
from pydantic import AnyUrl, FileUrl
55

66
from mcp_python.shared.session import BaseSession
77
from mcp_python.shared.version import SUPPORTED_PROTOCOL_VERSIONS
@@ -12,14 +12,21 @@
1212
ClientNotification,
1313
ClientRequest,
1414
ClientResult,
15+
CompleteResult,
1516
EmptyResult,
17+
GetPromptResult,
1618
Implementation,
1719
InitializedNotification,
1820
InitializeResult,
1921
JSONRPCMessage,
22+
ListPromptsResult,
2023
ListResourcesResult,
24+
ListRootsResult,
25+
ListToolsResult,
2126
LoggingLevel,
27+
PromptReference,
2228
ReadResourceResult,
29+
ResourceReference,
2330
ServerNotification,
2431
ServerRequest,
2532
)
@@ -61,7 +68,12 @@ async def initialize(self) -> InitializeResult:
6168
params=InitializeRequestParams(
6269
protocolVersion=LATEST_PROTOCOL_VERSION,
6370
capabilities=ClientCapabilities(
64-
sampling=None, experimental=None
71+
sampling=None,
72+
experimental=None,
73+
roots={
74+
# TODO: Should this be based on whether we _will_ send notifications, or only whether they're supported?
75+
"listChanged": True
76+
}
6577
),
6678
clientInfo=Implementation(name="mcp_python", version="0.1.0"),
6779
),
@@ -220,3 +232,72 @@ async def call_tool(
220232
),
221233
CallToolResult,
222234
)
235+
236+
async def list_prompts(self) -> ListPromptsResult:
237+
"""Send a prompts/list request."""
238+
from mcp_python.types import ListPromptsRequest
239+
240+
return await self.send_request(
241+
ClientRequest(
242+
ListPromptsRequest(
243+
method="prompts/list",
244+
)
245+
),
246+
ListPromptsResult,
247+
)
248+
249+
async def get_prompt(self, name: str, arguments: dict[str, str] | None = None) -> GetPromptResult:
250+
"""Send a prompts/get request."""
251+
from mcp_python.types import GetPromptRequest, GetPromptRequestParams
252+
253+
return await self.send_request(
254+
ClientRequest(
255+
GetPromptRequest(
256+
method="prompts/get",
257+
params=GetPromptRequestParams(name=name, arguments=arguments),
258+
)
259+
),
260+
GetPromptResult,
261+
)
262+
263+
async def complete(self, ref: ResourceReference | PromptReference, argument: dict) -> CompleteResult:
264+
"""Send a completion/complete request."""
265+
from mcp_python.types import CompleteRequest, CompleteRequestParams, CompletionArgument
266+
267+
return await self.send_request(
268+
ClientRequest(
269+
CompleteRequest(
270+
method="completion/complete",
271+
params=CompleteRequestParams(
272+
ref=ref,
273+
argument=CompletionArgument(**argument),
274+
),
275+
)
276+
),
277+
CompleteResult,
278+
)
279+
280+
async def list_tools(self) -> ListToolsResult:
281+
"""Send a tools/list request."""
282+
from mcp_python.types import ListToolsRequest
283+
284+
return await self.send_request(
285+
ClientRequest(
286+
ListToolsRequest(
287+
method="tools/list",
288+
)
289+
),
290+
ListToolsResult,
291+
)
292+
293+
async def send_roots_list_changed(self) -> None:
294+
"""Send a roots/list_changed notification."""
295+
from mcp_python.types import RootsListChangedNotification
296+
297+
await self.send_notification(
298+
ClientNotification(
299+
RootsListChangedNotification(
300+
method="notifications/roots/list_changed",
301+
)
302+
)
303+
)

mcp_python/server/session.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RequestResponder,
1313
)
1414
from mcp_python.types import (
15-
LATEST_PROTOCOL_VERSION,
15+
ListRootsResult, LATEST_PROTOCOL_VERSION,
1616
ClientNotification,
1717
ClientRequest,
1818
CreateMessageResult,
@@ -28,6 +28,10 @@
2828
ServerNotification,
2929
ServerRequest,
3030
ServerResult,
31+
ResourceListChangedNotification,
32+
ToolListChangedNotification,
33+
PromptListChangedNotification,
34+
ModelPreferences,
3135
)
3236

3337

@@ -142,6 +146,7 @@ async def request_create_message(
142146
temperature: float | None = None,
143147
stop_sequences: list[str] | None = None,
144148
metadata: dict[str, Any] | None = None,
149+
model_preferences: ModelPreferences | None = None,
145150
) -> CreateMessageResult:
146151
"""Send a sampling/create_message request."""
147152
from mcp_python.types import (
@@ -161,12 +166,26 @@ async def request_create_message(
161166
maxTokens=max_tokens,
162167
stopSequences=stop_sequences,
163168
metadata=metadata,
169+
modelPreferences=model_preferences,
164170
),
165171
)
166172
),
167173
CreateMessageResult,
168174
)
169175

176+
async def list_roots(self) -> ListRootsResult:
177+
"""Send a roots/list request."""
178+
from mcp_python.types import ListRootsRequest
179+
180+
return await self.send_request(
181+
ServerRequest(
182+
ListRootsRequest(
183+
method="roots/list",
184+
)
185+
),
186+
ListRootsResult,
187+
)
188+
170189
async def send_ping(self) -> EmptyResult:
171190
"""Send a ping request."""
172191
from mcp_python.types import PingRequest
@@ -198,3 +217,33 @@ async def send_progress_notification(
198217
)
199218
)
200219
)
220+
221+
async def send_resource_list_changed(self) -> None:
222+
"""Send a resource list changed notification."""
223+
await self.send_notification(
224+
ServerNotification(
225+
ResourceListChangedNotification(
226+
method="notifications/resources/list_changed",
227+
)
228+
)
229+
)
230+
231+
async def send_tool_list_changed(self) -> None:
232+
"""Send a tool list changed notification."""
233+
await self.send_notification(
234+
ServerNotification(
235+
ToolListChangedNotification(
236+
method="notifications/tools/list_changed",
237+
)
238+
)
239+
)
240+
241+
async def send_prompt_list_changed(self) -> None:
242+
"""Send a prompt list changed notification."""
243+
await self.send_notification(
244+
ServerNotification(
245+
PromptListChangedNotification(
246+
method="notifications/prompts/list_changed",
247+
)
248+
)
249+
)

0 commit comments

Comments
 (0)