Skip to content

Commit 22bd2eb

Browse files
committed
implement types as per 617 proposal
1 parent 6169282 commit 22bd2eb

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

src/mcp/server/session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ async def send_progress_notification(
301301
progress=progress,
302302
total=total,
303303
message=message,
304-
resource_uri=resource_uri,
304+
resourceUri=resource_uri,
305305
),
306306
)
307307
),

src/mcp/types.py

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
LATEST_PROTOCOL_VERSION = "2025-03-26"
3333

34+
AsyncToken = str | int
3435
ProgressToken = str | int
3536
Cursor = str
3637
Role = Literal["user", "assistant"]
@@ -260,6 +261,12 @@ class ToolsCapability(BaseModel):
260261
"""Whether this server supports notifications for changes to the tool list."""
261262
model_config = ConfigDict(extra="allow")
262263

264+
class AsyncCapability(BaseModel):
265+
"""Capability for async operations."""
266+
267+
maxKeepAliveTime: int | None = None
268+
"""The maximum keep alive time in seconds for async requests."""
269+
model_config = ConfigDict(extra="allow")
263270

264271
class LoggingCapability(BaseModel):
265272
"""Capability for logging operations."""
@@ -279,6 +286,9 @@ class ServerCapabilities(BaseModel):
279286
resources: ResourcesCapability | None = None
280287
"""Present if the server offers any resources to read."""
281288
tools: ToolsCapability | None = None
289+
"""Present if the server offers async tool calling support."""
290+
async_: AsyncCapability | None = Field(alias='async', default=None)
291+
282292
"""Present if the server offers any tools to call."""
283293
model_config = ConfigDict(extra="allow")
284294

@@ -356,7 +366,7 @@ class ProgressNotificationParams(NotificationParams):
356366
Message related to progress. This should provide relevant human readable
357367
progress information.
358368
"""
359-
resource_uri: Annotated[AnyUrl, UrlConstraints(host_required=False)] | None = None
369+
resourceUri: Annotated[AnyUrl, UrlConstraints(host_required=False)] | None = None
360370
"""
361371
An optional reference to an ephemeral resource associated with this
362372
progress, servers may delete these at their descretion, but are encouraged
@@ -767,6 +777,12 @@ class ToolAnnotations(BaseModel):
767777
of a memory tool is not.
768778
Default: true
769779
"""
780+
preferAsync: bool | None = None
781+
"""
782+
If true, should ideally be called using the async protocol
783+
as requests are expected to be long running.
784+
Default: false
785+
"""
770786
model_config = ConfigDict(extra="allow")
771787

772788

@@ -797,19 +813,61 @@ class CallToolRequestParams(RequestParams):
797813
arguments: dict[str, Any] | None = None
798814
model_config = ConfigDict(extra="allow")
799815

800-
801816
class CallToolRequest(Request[CallToolRequestParams, Literal["tools/call"]]):
802817
"""Used by the client to invoke a tool provided by the server."""
803818

804819
method: Literal["tools/call"]
805820
params: CallToolRequestParams
806821

822+
class CallToolAsyncRequestParams(CallToolRequestParams):
823+
"""Parameters for calling a tool asynchronously."""
824+
825+
keepAlive: int | None = None
826+
model_config = ConfigDict(extra="allow")
827+
828+
class CallToolAsyncRequest(Request[CallToolAsyncRequestParams, Literal["tools/async/call"]]):
829+
"""Used by the client to invoke a tool provided by the server asynchronously."""
830+
method: Literal["tools/async/call"]
831+
params: CallToolAsyncRequestParams
832+
833+
class JoinCallToolRequestParams(RequestParams):
834+
"""Parameters for joining an asynchronous tool call."""
835+
token: AsyncToken
836+
keepAlive: int | None = None
837+
model_config = ConfigDict(extra="allow")
838+
839+
class JoinCallToolAsyncRequest(Request[JoinCallToolRequestParams, Literal["tools/async/join"]]):
840+
"""Used by the client to join an tool call executing on the server asynchronously."""
841+
method: Literal["tools/async/join"]
842+
params: JoinCallToolRequestParams
843+
844+
class CancelToolAsyncNotificationParams(NotificationParams):
845+
token: AsyncToken
846+
847+
class CancelToolAsyncNotification(Notification[CancelToolAsyncNotificationParams, Literal["tools/async/cancel"]]):
848+
method: Literal["tools/async/cancel"]
849+
params: CancelToolAsyncNotificationParams
850+
851+
class GetToolAsyncResultRequestParams(RequestParams):
852+
token: AsyncToken
853+
854+
class GetToolAsyncResultRequest(Request[GetToolAsyncResultRequestParams, Literal["tools/async/get"]]):
855+
method: Literal["tools/async/get"]
856+
params: GetToolAsyncResultRequestParams
807857

808858
class CallToolResult(Result):
809859
"""The server's response to a tool call."""
810860

811861
content: list[TextContent | ImageContent | EmbeddedResource]
812862
isError: bool = False
863+
isPending: bool = False
864+
865+
class CallToolAsyncResult(Result):
866+
"""The servers response to an async tool call"""
867+
token: AsyncToken | None = None
868+
recieved: int | None = None
869+
keepAlive: int | None = None
870+
accepted: bool
813871

814872

815873
class ToolListChangedNotification(
@@ -1141,6 +1199,8 @@ class ClientRequest(
11411199
| SubscribeRequest
11421200
| UnsubscribeRequest
11431201
| CallToolRequest
1202+
| CallToolAsyncRequest
1203+
| JoinCallToolAsyncRequest
11441204
| ListToolsRequest
11451205
]
11461206
):
@@ -1152,6 +1212,7 @@ class ClientNotification(
11521212
CancelledNotification
11531213
| ProgressNotification
11541214
| InitializedNotification
1215+
| CancelToolAsyncNotification
11551216
| RootsListChangedNotification
11561217
]
11571218
):
@@ -1191,6 +1252,7 @@ class ServerResult(
11911252
| ListResourceTemplatesResult
11921253
| ReadResourceResult
11931254
| CallToolResult
1255+
| CallToolAsyncResult
11941256
| ListToolsResult
11951257
]
11961258
):

0 commit comments

Comments
 (0)