Skip to content

Commit cbc2c20

Browse files
refactor: extract BaseMetadata base class for name/title fields
Add BaseMetadata base class to provide common name and title fields for consistency with TypeScript SDK implementation. This centralizes the field definitions for Implementation, Tool, Resource, ResourceTemplate, and Prompt classes. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 2c2feea commit cbc2c20

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed

src/mcp/types.py

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,22 @@ class EmptyResult(Result):
196196
"""A response that indicates success but carries no data."""
197197

198198

199-
class Implementation(BaseModel):
200-
"""Describes the name and version of an MCP implementation."""
201-
199+
# BaseMetadata provides common name and title fields for consistency with TypeScript SDK.
200+
# I'm not sure of the value of extracting this to another class and add more inheritance,
201+
# it feels like additional indirection but being consistent with typescript here.
202+
class BaseMetadata(BaseModel):
203+
"""Base class for entities with name and optional title fields."""
204+
202205
name: str
206+
"""The programmatic name of the entity."""
207+
203208
title: str | None = None
204-
"""A human-readable title for the implementation."""
209+
"""An optional human-readable title for the entity."""
210+
211+
212+
class Implementation(BaseMetadata):
213+
"""Describes the name and version of an MCP implementation."""
214+
205215
version: str
206216
model_config = ConfigDict(extra="allow")
207217

@@ -384,15 +394,11 @@ class Annotations(BaseModel):
384394
model_config = ConfigDict(extra="allow")
385395

386396

387-
class Resource(BaseModel):
397+
class Resource(BaseMetadata):
388398
"""A known resource that the server is capable of reading."""
389399

390400
uri: Annotated[AnyUrl, UrlConstraints(host_required=False)]
391401
"""The URI of this resource."""
392-
name: str
393-
"""A human-readable name for this resource."""
394-
title: str | None = None
395-
"""A human-readable title for this resource."""
396402
description: str | None = None
397403
"""A description of what this resource represents."""
398404
mimeType: str | None = None
@@ -413,18 +419,14 @@ class Resource(BaseModel):
413419
model_config = ConfigDict(extra="allow")
414420

415421

416-
class ResourceTemplate(BaseModel):
422+
class ResourceTemplate(BaseMetadata):
417423
"""A template description for resources available on the server."""
418424

419425
uriTemplate: str
420426
"""
421427
A URI template (according to RFC 6570) that can be used to construct resource
422428
URIs.
423429
"""
424-
name: str
425-
"""A human-readable name for the type of resource this template refers to."""
426-
title: str | None = None
427-
"""A human-readable title for the type of resource this template refers to."""
428430
description: str | None = None
429431
"""A human-readable description of what this template is for."""
430432
mimeType: str | None = None
@@ -607,13 +609,8 @@ class PromptArgument(BaseModel):
607609
model_config = ConfigDict(extra="allow")
608610

609611

610-
class Prompt(BaseModel):
612+
class Prompt(BaseMetadata):
611613
"""A prompt or prompt template that the server offers."""
612-
613-
name: str
614-
"""The name of the prompt or prompt template."""
615-
title: str | None = None
616-
"""A human-readable title for the prompt."""
617614
description: str | None = None
618615
"""An optional description of what this prompt provides."""
619616
arguments: list[PromptArgument] | None = None
@@ -816,13 +813,8 @@ class ToolAnnotations(BaseModel):
816813
model_config = ConfigDict(extra="allow")
817814

818815

819-
class Tool(BaseModel):
816+
class Tool(BaseMetadata):
820817
"""Definition for a tool the client can call."""
821-
822-
name: str
823-
"""The name of the tool."""
824-
title: str | None = None
825-
"""A human-readable title for the tool."""
826818
description: str | None = None
827819
"""A human-readable description of the tool."""
828820
inputSchema: dict[str, Any]

0 commit comments

Comments
 (0)