Skip to content

Commit 4066cd8

Browse files
committed
feat: add structured capability types
Replace generic capability dictionaries with structured types for prompts, resources, tools, and roots. This improves type safety and makes capability features like listChanged and subscribe more explicit in the protocol.
1 parent 60e9c7a commit 4066cd8

File tree

3 files changed

+59
-10
lines changed

3 files changed

+59
-10
lines changed

mcp_python/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@
3333
Notification,
3434
PingRequest,
3535
ProgressNotification,
36+
PromptsCapability,
3637
ReadResourceRequest,
3738
ReadResourceResult,
3839
Resource,
40+
ResourcesCapability,
3941
ResourceUpdatedNotification,
42+
RootsCapability,
4043
SamplingMessage,
4144
ServerCapabilities,
4245
ServerNotification,
@@ -46,6 +49,7 @@
4649
StopReason,
4750
SubscribeRequest,
4851
Tool,
52+
ToolsCapability,
4953
UnsubscribeRequest,
5054
)
5155
from .types import (
@@ -82,10 +86,13 @@
8286
"Notification",
8387
"PingRequest",
8488
"ProgressNotification",
89+
"PromptsCapability",
8590
"ReadResourceRequest",
8691
"ReadResourceResult",
92+
"ResourcesCapability",
8793
"ResourceUpdatedNotification",
8894
"Resource",
95+
"RootsCapability",
8996
"SamplingMessage",
9097
"SamplingRole",
9198
"ServerCapabilities",
@@ -98,6 +105,7 @@
98105
"StopReason",
99106
"SubscribeRequest",
100107
"Tool",
108+
"ToolsCapability",
101109
"UnsubscribeRequest",
102110
"stdio_client",
103111
"stdio_server",

mcp_python/server/__init__.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,24 @@
2727
ListResourcesResult,
2828
ListToolsRequest,
2929
ListToolsResult,
30+
LoggingCapability,
3031
LoggingLevel,
3132
PingRequest,
3233
ProgressNotification,
3334
Prompt,
3435
PromptReference,
36+
PromptsCapability,
3537
ReadResourceRequest,
3638
ReadResourceResult,
3739
Resource,
3840
ResourceReference,
41+
ResourcesCapability,
3942
ServerCapabilities,
4043
ServerResult,
4144
SetLevelRequest,
4245
SubscribeRequest,
4346
Tool,
47+
ToolsCapability,
4448
UnsubscribeRequest,
4549
)
4650

@@ -84,14 +88,14 @@ def pkg_version(package: str) -> str:
8488
def get_capabilities(self) -> ServerCapabilities:
8589
"""Convert existing handlers to a ServerCapabilities object."""
8690

87-
def get_capability(req_type: type) -> dict[str, Any] | None:
88-
return {} if req_type in self.request_handlers else None
91+
def has_capability(req_type: type) -> bool:
92+
return req_type in self.request_handlers
8993

9094
return ServerCapabilities(
91-
prompts=get_capability(ListPromptsRequest),
92-
resources=get_capability(ListResourcesRequest),
93-
tools=get_capability(ListToolsRequest),
94-
logging=get_capability(SetLevelRequest),
95+
prompts=PromptsCapability(listChanged=False) if has_capability(ListPromptsRequest) else None,
96+
resources=ResourcesCapability(subscribe=False, listChanged=False) if has_capability(ListResourcesRequest) else None,
97+
tools=ToolsCapability(listChanged=False) if has_capability(ListToolsRequest) else None,
98+
logging=LoggingCapability() if has_capability(SetLevelRequest) else None,
9599
)
96100

97101
@property

mcp_python/types.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,28 +184,65 @@ class Implementation(BaseModel):
184184
model_config = ConfigDict(extra="allow")
185185

186186

187+
class RootsCapability(BaseModel):
188+
"""Capability for root operations."""
189+
listChanged: bool | None = None
190+
"""Whether the client supports notifications for changes to the roots list."""
191+
model_config = ConfigDict(extra="allow")
192+
193+
187194
class ClientCapabilities(BaseModel):
188195
"""Capabilities a client may support."""
189196

190197
experimental: dict[str, dict[str, Any]] | None = None
191198
"""Experimental, non-standard capabilities that the client supports."""
192199
sampling: dict[str, Any] | None = None
193200
"""Present if the client supports sampling from an LLM."""
201+
roots: RootsCapability | None = None
202+
"""Present if the client supports listing roots."""
203+
model_config = ConfigDict(extra="allow")
204+
205+
206+
class PromptsCapability(BaseModel):
207+
"""Capability for prompts operations."""
208+
listChanged: bool | None = None
209+
"""Whether this server supports notifications for changes to the prompt list."""
194210
model_config = ConfigDict(extra="allow")
195211

196212

213+
class ResourcesCapability(BaseModel):
214+
"""Capability for resources operations."""
215+
subscribe: bool | None = None
216+
"""Whether this server supports subscribing to resource updates."""
217+
listChanged: bool | None = None
218+
"""Whether this server supports notifications for changes to the resource list."""
219+
model_config = ConfigDict(extra="allow")
220+
221+
222+
class ToolsCapability(BaseModel):
223+
"""Capability for tools operations."""
224+
listChanged: bool | None = None
225+
"""Whether this server supports notifications for changes to the tool list."""
226+
model_config = ConfigDict(extra="allow")
227+
228+
229+
class LoggingCapability(BaseModel):
230+
"""Capability for logging operations."""
231+
pass
232+
233+
197234
class ServerCapabilities(BaseModel):
198235
"""Capabilities that a server may support."""
199236

200237
experimental: dict[str, dict[str, Any]] | None = None
201238
"""Experimental, non-standard capabilities that the server supports."""
202-
logging: dict[str, Any] | None = None
239+
logging: LoggingCapability | None = None
203240
"""Present if the server supports sending log messages to the client."""
204-
prompts: dict[str, Any] | None = None
241+
prompts: PromptsCapability | None = None
205242
"""Present if the server offers any prompt templates."""
206-
resources: dict[str, Any] | None = None
243+
resources: ResourcesCapability | None = None
207244
"""Present if the server offers any resources to read."""
208-
tools: dict[str, Any] | None = None
245+
tools: ToolsCapability | None = None
209246
"""Present if the server offers any tools to call."""
210247
model_config = ConfigDict(extra="allow")
211248

0 commit comments

Comments
 (0)