forked from modelcontextprotocol/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.py
More file actions
211 lines (187 loc) · 6.15 KB
/
session.py
File metadata and controls
211 lines (187 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream
from pydantic import AnyUrl
from mcp_python.shared.session import BaseSession
from mcp_python.shared.version import SUPPORTED_PROTOCOL_VERSION
from mcp_python.types import (
CallToolResult,
ClientCapabilities,
ClientNotification,
ClientRequest,
ClientResult,
EmptyResult,
Implementation,
InitializedNotification,
InitializeResult,
JSONRPCMessage,
ListResourcesResult,
LoggingLevel,
ReadResourceResult,
ServerNotification,
ServerRequest,
)
class ClientSession(
BaseSession[
ClientRequest,
ClientNotification,
ClientResult,
ServerRequest,
ServerNotification,
]
):
def __init__(
self,
read_stream: MemoryObjectReceiveStream[JSONRPCMessage | Exception],
write_stream: MemoryObjectSendStream[JSONRPCMessage],
) -> None:
super().__init__(read_stream, write_stream, ServerRequest, ServerNotification)
async def initialize(self) -> InitializeResult:
from mcp_python.types import (
InitializeRequest,
InitializeRequestParams,
)
result = await self.send_request(
ClientRequest(
InitializeRequest(
method="initialize",
params=InitializeRequestParams(
protocolVersion=SUPPORTED_PROTOCOL_VERSION,
capabilities=ClientCapabilities(
sampling=None, experimental=None
),
clientInfo=Implementation(name="mcp_python", version="0.1.0"),
),
)
),
InitializeResult,
)
if result.protocolVersion != SUPPORTED_PROTOCOL_VERSION:
raise RuntimeError(
f"Unsupported protocol version from the server: {result.protocolVersion}"
)
await self.send_notification(
ClientNotification(
InitializedNotification(method="notifications/initialized")
)
)
return result
async def send_ping(self) -> EmptyResult:
"""Send a ping request."""
from mcp_python.types import PingRequest
return await self.send_request(
ClientRequest(
PingRequest(
method="ping",
)
),
EmptyResult,
)
async def send_progress_notification(
self, progress_token: str | int, progress: float, total: float | None = None
) -> None:
"""Send a progress notification."""
from mcp_python.types import (
ProgressNotification,
ProgressNotificationParams,
)
await self.send_notification(
ClientNotification(
ProgressNotification(
method="notifications/progress",
params=ProgressNotificationParams(
progressToken=progress_token,
progress=progress,
total=total,
),
),
)
)
async def set_logging_level(self, level: LoggingLevel) -> EmptyResult:
"""Send a logging/setLevel request."""
from mcp_python.types import (
SetLevelRequest,
SetLevelRequestParams,
)
return await self.send_request(
ClientRequest(
SetLevelRequest(
method="logging/setLevel",
params=SetLevelRequestParams(level=level),
)
),
EmptyResult,
)
async def list_resources(self) -> ListResourcesResult:
"""Send a resources/list request."""
from mcp_python.types import (
ListResourcesRequest,
)
return await self.send_request(
ClientRequest(
ListResourcesRequest(
method="resources/list",
)
),
ListResourcesResult,
)
async def read_resource(self, uri: AnyUrl) -> ReadResourceResult:
"""Send a resources/read request."""
from mcp_python.types import (
ReadResourceRequest,
ReadResourceRequestParams,
)
return await self.send_request(
ClientRequest(
ReadResourceRequest(
method="resources/read",
params=ReadResourceRequestParams(uri=uri),
)
),
ReadResourceResult,
)
async def subscribe_resource(self, uri: AnyUrl) -> EmptyResult:
"""Send a resources/subscribe request."""
from mcp_python.types import (
SubscribeRequest,
SubscribeRequestParams,
)
return await self.send_request(
ClientRequest(
SubscribeRequest(
method="resources/subscribe",
params=SubscribeRequestParams(uri=uri),
)
),
EmptyResult,
)
async def unsubscribe_resource(self, uri: AnyUrl) -> EmptyResult:
"""Send a resources/unsubscribe request."""
from mcp_python.types import (
UnsubscribeRequest,
UnsubscribeRequestParams,
)
return await self.send_request(
ClientRequest(
UnsubscribeRequest(
method="resources/unsubscribe",
params=UnsubscribeRequestParams(uri=uri),
)
),
EmptyResult,
)
async def call_tool(
self, name: str, arguments: dict | None = None
) -> CallToolResult:
"""Send a tools/call request."""
from mcp_python.types import (
CallToolRequest,
CallToolRequestParams,
)
return await self.send_request(
ClientRequest(
CallToolRequest(
method="tools/call",
params=CallToolRequestParams(name=name, arguments=arguments),
)
),
CallToolResult,
)