3
3
from anyio .streams .memory import MemoryObjectReceiveStream , MemoryObjectSendStream
4
4
from pydantic import AnyUrl
5
5
6
+ import mcp .types as types
6
7
from mcp .shared .session import BaseSession
7
8
from mcp .shared .version import SUPPORTED_PROTOCOL_VERSIONS
8
- from mcp .types import (
9
- LATEST_PROTOCOL_VERSION ,
10
- CallToolResult ,
11
- ClientCapabilities ,
12
- ClientNotification ,
13
- ClientRequest ,
14
- ClientResult ,
15
- CompleteResult ,
16
- EmptyResult ,
17
- GetPromptResult ,
18
- Implementation ,
19
- InitializedNotification ,
20
- InitializeResult ,
21
- JSONRPCMessage ,
22
- ListPromptsResult ,
23
- ListResourcesResult ,
24
- ListToolsResult ,
25
- LoggingLevel ,
26
- PromptReference ,
27
- ReadResourceResult ,
28
- ResourceReference ,
29
- RootsCapability ,
30
- ServerNotification ,
31
- ServerRequest ,
32
- )
33
9
34
10
35
11
class ClientSession (
36
12
BaseSession [
37
- ClientRequest ,
38
- ClientNotification ,
39
- ClientResult ,
40
- ServerRequest ,
41
- ServerNotification ,
13
+ types . ClientRequest ,
14
+ types . ClientNotification ,
15
+ types . ClientResult ,
16
+ types . ServerRequest ,
17
+ types . ServerNotification ,
42
18
]
43
19
):
44
20
def __init__ (
45
21
self ,
46
- read_stream : MemoryObjectReceiveStream [JSONRPCMessage | Exception ],
47
- write_stream : MemoryObjectSendStream [JSONRPCMessage ],
22
+ read_stream : MemoryObjectReceiveStream [types . JSONRPCMessage | Exception ],
23
+ write_stream : MemoryObjectSendStream [types . JSONRPCMessage ],
48
24
read_timeout_seconds : timedelta | None = None ,
49
25
) -> None :
50
26
super ().__init__ (
51
27
read_stream ,
52
28
write_stream ,
53
- ServerRequest ,
54
- ServerNotification ,
29
+ types . ServerRequest ,
30
+ types . ServerNotification ,
55
31
read_timeout_seconds = read_timeout_seconds ,
56
32
)
57
33
58
- async def initialize (self ) -> InitializeResult :
59
- from mcp .types import (
60
- InitializeRequest ,
61
- InitializeRequestParams ,
62
- )
63
-
34
+ async def initialize (self ) -> types .InitializeResult :
64
35
result = await self .send_request (
65
- ClientRequest (
66
- InitializeRequest (
36
+ types . ClientRequest (
37
+ types . InitializeRequest (
67
38
method = "initialize" ,
68
- params = InitializeRequestParams (
69
- protocolVersion = LATEST_PROTOCOL_VERSION ,
70
- capabilities = ClientCapabilities (
39
+ params = types . InitializeRequestParams (
40
+ protocolVersion = types . LATEST_PROTOCOL_VERSION ,
41
+ capabilities = types . ClientCapabilities (
71
42
sampling = None ,
72
43
experimental = None ,
73
- roots = RootsCapability (
44
+ roots = types . RootsCapability (
74
45
# TODO: Should this be based on whether we
75
46
# _will_ send notifications, or only whether
76
47
# they're supported?
77
48
listChanged = True
78
49
),
79
50
),
80
- clientInfo = Implementation (name = "mcp" , version = "0.1.0" ),
51
+ clientInfo = types . Implementation (name = "mcp" , version = "0.1.0" ),
81
52
),
82
53
)
83
54
),
84
- InitializeResult ,
55
+ types . InitializeResult ,
85
56
)
86
57
87
58
if result .protocolVersion not in SUPPORTED_PROTOCOL_VERSIONS :
@@ -91,40 +62,33 @@ async def initialize(self) -> InitializeResult:
91
62
)
92
63
93
64
await self .send_notification (
94
- ClientNotification (
95
- InitializedNotification (method = "notifications/initialized" )
65
+ types . ClientNotification (
66
+ types . InitializedNotification (method = "notifications/initialized" )
96
67
)
97
68
)
98
69
99
70
return result
100
71
101
- async def send_ping (self ) -> EmptyResult :
72
+ async def send_ping (self ) -> types . EmptyResult :
102
73
"""Send a ping request."""
103
- from mcp .types import PingRequest
104
-
105
74
return await self .send_request (
106
- ClientRequest (
107
- PingRequest (
75
+ types . ClientRequest (
76
+ types . PingRequest (
108
77
method = "ping" ,
109
78
)
110
79
),
111
- EmptyResult ,
80
+ types . EmptyResult ,
112
81
)
113
82
114
83
async def send_progress_notification (
115
84
self , progress_token : str | int , progress : float , total : float | None = None
116
85
) -> None :
117
86
"""Send a progress notification."""
118
- from mcp .types import (
119
- ProgressNotification ,
120
- ProgressNotificationParams ,
121
- )
122
-
123
87
await self .send_notification (
124
- ClientNotification (
125
- ProgressNotification (
88
+ types . ClientNotification (
89
+ types . ProgressNotification (
126
90
method = "notifications/progress" ,
127
- params = ProgressNotificationParams (
91
+ params = types . ProgressNotificationParams (
128
92
progressToken = progress_token ,
129
93
progress = progress ,
130
94
total = total ,
@@ -133,180 +97,137 @@ async def send_progress_notification(
133
97
)
134
98
)
135
99
136
- async def set_logging_level (self , level : LoggingLevel ) -> EmptyResult :
100
+ async def set_logging_level (self , level : types . LoggingLevel ) -> types . EmptyResult :
137
101
"""Send a logging/setLevel request."""
138
- from mcp .types import (
139
- SetLevelRequest ,
140
- SetLevelRequestParams ,
141
- )
142
-
143
102
return await self .send_request (
144
- ClientRequest (
145
- SetLevelRequest (
103
+ types . ClientRequest (
104
+ types . SetLevelRequest (
146
105
method = "logging/setLevel" ,
147
- params = SetLevelRequestParams (level = level ),
106
+ params = types . SetLevelRequestParams (level = level ),
148
107
)
149
108
),
150
- EmptyResult ,
109
+ types . EmptyResult ,
151
110
)
152
111
153
- async def list_resources (self ) -> ListResourcesResult :
112
+ async def list_resources (self ) -> types . ListResourcesResult :
154
113
"""Send a resources/list request."""
155
- from mcp .types import (
156
- ListResourcesRequest ,
157
- )
158
-
159
114
return await self .send_request (
160
- ClientRequest (
161
- ListResourcesRequest (
115
+ types . ClientRequest (
116
+ types . ListResourcesRequest (
162
117
method = "resources/list" ,
163
118
)
164
119
),
165
- ListResourcesResult ,
120
+ types . ListResourcesResult ,
166
121
)
167
122
168
- async def read_resource (self , uri : AnyUrl ) -> ReadResourceResult :
123
+ async def read_resource (self , uri : AnyUrl ) -> types . ReadResourceResult :
169
124
"""Send a resources/read request."""
170
- from mcp .types import (
171
- ReadResourceRequest ,
172
- ReadResourceRequestParams ,
173
- )
174
-
175
125
return await self .send_request (
176
- ClientRequest (
177
- ReadResourceRequest (
126
+ types . ClientRequest (
127
+ types . ReadResourceRequest (
178
128
method = "resources/read" ,
179
- params = ReadResourceRequestParams (uri = uri ),
129
+ params = types . ReadResourceRequestParams (uri = uri ),
180
130
)
181
131
),
182
- ReadResourceResult ,
132
+ types . ReadResourceResult ,
183
133
)
184
134
185
- async def subscribe_resource (self , uri : AnyUrl ) -> EmptyResult :
135
+ async def subscribe_resource (self , uri : AnyUrl ) -> types . EmptyResult :
186
136
"""Send a resources/subscribe request."""
187
- from mcp .types import (
188
- SubscribeRequest ,
189
- SubscribeRequestParams ,
190
- )
191
-
192
137
return await self .send_request (
193
- ClientRequest (
194
- SubscribeRequest (
138
+ types . ClientRequest (
139
+ types . SubscribeRequest (
195
140
method = "resources/subscribe" ,
196
- params = SubscribeRequestParams (uri = uri ),
141
+ params = types . SubscribeRequestParams (uri = uri ),
197
142
)
198
143
),
199
- EmptyResult ,
144
+ types . EmptyResult ,
200
145
)
201
146
202
- async def unsubscribe_resource (self , uri : AnyUrl ) -> EmptyResult :
147
+ async def unsubscribe_resource (self , uri : AnyUrl ) -> types . EmptyResult :
203
148
"""Send a resources/unsubscribe request."""
204
- from mcp .types import (
205
- UnsubscribeRequest ,
206
- UnsubscribeRequestParams ,
207
- )
208
-
209
149
return await self .send_request (
210
- ClientRequest (
211
- UnsubscribeRequest (
150
+ types . ClientRequest (
151
+ types . UnsubscribeRequest (
212
152
method = "resources/unsubscribe" ,
213
- params = UnsubscribeRequestParams (uri = uri ),
153
+ params = types . UnsubscribeRequestParams (uri = uri ),
214
154
)
215
155
),
216
- EmptyResult ,
156
+ types . EmptyResult ,
217
157
)
218
158
219
159
async def call_tool (
220
160
self , name : str , arguments : dict | None = None
221
- ) -> CallToolResult :
161
+ ) -> types . CallToolResult :
222
162
"""Send a tools/call request."""
223
- from mcp .types import (
224
- CallToolRequest ,
225
- CallToolRequestParams ,
226
- )
227
-
228
163
return await self .send_request (
229
- ClientRequest (
230
- CallToolRequest (
164
+ types . ClientRequest (
165
+ types . CallToolRequest (
231
166
method = "tools/call" ,
232
- params = CallToolRequestParams (name = name , arguments = arguments ),
167
+ params = types . CallToolRequestParams (name = name , arguments = arguments ),
233
168
)
234
169
),
235
- CallToolResult ,
170
+ types . CallToolResult ,
236
171
)
237
172
238
- async def list_prompts (self ) -> ListPromptsResult :
173
+ async def list_prompts (self ) -> types . ListPromptsResult :
239
174
"""Send a prompts/list request."""
240
- from mcp .types import ListPromptsRequest
241
-
242
175
return await self .send_request (
243
- ClientRequest (
244
- ListPromptsRequest (
176
+ types . ClientRequest (
177
+ types . ListPromptsRequest (
245
178
method = "prompts/list" ,
246
179
)
247
180
),
248
- ListPromptsResult ,
181
+ types . ListPromptsResult ,
249
182
)
250
183
251
184
async def get_prompt (
252
185
self , name : str , arguments : dict [str , str ] | None = None
253
- ) -> GetPromptResult :
186
+ ) -> types . GetPromptResult :
254
187
"""Send a prompts/get request."""
255
- from mcp .types import GetPromptRequest , GetPromptRequestParams
256
-
257
188
return await self .send_request (
258
- ClientRequest (
259
- GetPromptRequest (
189
+ types . ClientRequest (
190
+ types . GetPromptRequest (
260
191
method = "prompts/get" ,
261
- params = GetPromptRequestParams (name = name , arguments = arguments ),
192
+ params = types . GetPromptRequestParams (name = name , arguments = arguments ),
262
193
)
263
194
),
264
- GetPromptResult ,
195
+ types . GetPromptResult ,
265
196
)
266
197
267
198
async def complete (
268
- self , ref : ResourceReference | PromptReference , argument : dict
269
- ) -> CompleteResult :
199
+ self , ref : types . ResourceReference | types . PromptReference , argument : dict
200
+ ) -> types . CompleteResult :
270
201
"""Send a completion/complete request."""
271
- from mcp .types import (
272
- CompleteRequest ,
273
- CompleteRequestParams ,
274
- CompletionArgument ,
275
- )
276
-
277
202
return await self .send_request (
278
- ClientRequest (
279
- CompleteRequest (
203
+ types . ClientRequest (
204
+ types . CompleteRequest (
280
205
method = "completion/complete" ,
281
- params = CompleteRequestParams (
206
+ params = types . CompleteRequestParams (
282
207
ref = ref ,
283
- argument = CompletionArgument (** argument ),
208
+ argument = types . CompletionArgument (** argument ),
284
209
),
285
210
)
286
211
),
287
- CompleteResult ,
212
+ types . CompleteResult ,
288
213
)
289
214
290
- async def list_tools (self ) -> ListToolsResult :
215
+ async def list_tools (self ) -> types . ListToolsResult :
291
216
"""Send a tools/list request."""
292
- from mcp .types import ListToolsRequest
293
-
294
217
return await self .send_request (
295
- ClientRequest (
296
- ListToolsRequest (
218
+ types . ClientRequest (
219
+ types . ListToolsRequest (
297
220
method = "tools/list" ,
298
221
)
299
222
),
300
- ListToolsResult ,
223
+ types . ListToolsResult ,
301
224
)
302
225
303
226
async def send_roots_list_changed (self ) -> None :
304
227
"""Send a roots/list_changed notification."""
305
- from mcp .types import RootsListChangedNotification
306
-
307
228
await self .send_notification (
308
- ClientNotification (
309
- RootsListChangedNotification (
229
+ types . ClientNotification (
230
+ types . RootsListChangedNotification (
310
231
method = "notifications/roots/list_changed" ,
311
232
)
312
233
)
0 commit comments