Skip to content

Commit ec8d9a2

Browse files
committed
updated types
1 parent eb0ccf9 commit ec8d9a2

File tree

6 files changed

+16
-17
lines changed

6 files changed

+16
-17
lines changed

examples/servers/simple-pagination/mcp_simple_pagination/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
SAMPLE_RESOURCES = [
2929
types.Resource(
30-
uri=AnyUrl(f"file:///path/to/resource_{i}.txt"),
30+
uri=f"file:///path/to/resource_{i}.txt",
3131
name=f"resource_{i}",
3232
description=f"This is sample resource number {i}",
3333
)

examples/servers/simple-resource/mcp_simple_resource/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def main(port: int, transport: str) -> int:
3737
async def list_resources() -> list[types.Resource]:
3838
return [
3939
types.Resource(
40-
uri=FileUrl(f"file:///{name}.txt"),
40+
uri=f"file:///{name}.txt",
4141
name=name,
4242
title=SAMPLE_RESOURCES[name]["title"],
4343
description=f"A sample text resource named {name}",

examples/snippets/servers/pagination_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async def list_resources_paginated(request: types.ListResourcesRequest) -> types
2828

2929
# Get page of resources
3030
page_items = [
31-
types.Resource(uri=AnyUrl(f"resource://items/{item}"), name=item, description=f"Description for {item}")
31+
types.Resource(uri=f"resource://items/{item}", name=item, description=f"Description for {item}")
3232
for item in ITEMS[start:end]
3333
]
3434

src/mcp/client/session.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import anyio.lowlevel
66
from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream
77
from jsonschema import SchemaError, ValidationError, validate
8-
from pydantic import AnyUrl, TypeAdapter
8+
from pydantic import TypeAdapter
99
from typing_extensions import deprecated
1010

1111
import mcp.types as types
@@ -299,7 +299,7 @@ async def list_resource_templates(
299299
types.ListResourceTemplatesResult,
300300
)
301301

302-
async def read_resource(self, uri: AnyUrl) -> types.ReadResourceResult:
302+
async def read_resource(self, uri: str) -> types.ReadResourceResult:
303303
"""Send a resources/read request."""
304304
return await self.send_request(
305305
types.ClientRequest(
@@ -310,7 +310,7 @@ async def read_resource(self, uri: AnyUrl) -> types.ReadResourceResult:
310310
types.ReadResourceResult,
311311
)
312312

313-
async def subscribe_resource(self, uri: AnyUrl) -> types.EmptyResult:
313+
async def subscribe_resource(self, uri: str) -> types.EmptyResult:
314314
"""Send a resources/subscribe request."""
315315
return await self.send_request(
316316
types.ClientRequest(
@@ -321,7 +321,7 @@ async def subscribe_resource(self, uri: AnyUrl) -> types.EmptyResult:
321321
types.EmptyResult,
322322
)
323323

324-
async def unsubscribe_resource(self, uri: AnyUrl) -> types.EmptyResult:
324+
async def unsubscribe_resource(self, uri: str) -> types.EmptyResult:
325325
"""Send a resources/unsubscribe request."""
326326
return await self.send_request(
327327
types.ClientRequest(

src/mcp/server/fastmcp/resources/base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from typing import Annotated
55

66
from pydantic import (
7-
AnyUrl,
87
BaseModel,
98
ConfigDict,
109
Field,
@@ -21,7 +20,7 @@ class Resource(BaseModel, abc.ABC):
2120

2221
model_config = ConfigDict(validate_default=True)
2322

24-
uri: Annotated[AnyUrl, UrlConstraints(host_required=False)] = Field(default=..., description="URI of the resource")
23+
uri: Annotated[str, UrlConstraints(host_required=False)] = Field(default=..., description="URI of the resource")
2524
name: str | None = Field(description="Name of the resource", default=None)
2625
title: str | None = Field(description="Human-readable title of the resource", default=None)
2726
description: str | None = Field(description="Description of the resource", default=None)

src/mcp/types.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from collections.abc import Callable
22
from typing import Annotated, Any, Generic, Literal, TypeAlias, TypeVar
33

4-
from pydantic import BaseModel, ConfigDict, Field, FileUrl, RootModel
4+
from pydantic import BaseModel, ConfigDict, Field, FileUrl, RootModel, UrlConstraints
55
from typing_extensions import deprecated
66

77
"""
@@ -430,7 +430,7 @@ class Annotations(BaseModel):
430430
class Resource(BaseMetadata):
431431
"""A known resource that the server is capable of reading."""
432432

433-
uri: str
433+
Annotated[str, UrlConstraints(host_required=False)]
434434
"""The URI of this resource."""
435435
description: str | None = None
436436
"""A description of what this resource represents."""
@@ -501,7 +501,7 @@ class ListResourceTemplatesResult(PaginatedResult):
501501
class ReadResourceRequestParams(RequestParams):
502502
"""Parameters for reading a resource."""
503503

504-
uri: str
504+
Annotated[str, UrlConstraints(host_required=False)]
505505
"""
506506
The URI of the resource to read. The URI can use any protocol; it is up to the
507507
server how to interpret it.
@@ -519,7 +519,7 @@ class ReadResourceRequest(Request[ReadResourceRequestParams, Literal["resources/
519519
class ResourceContents(BaseModel):
520520
"""The contents of a specific resource or sub-resource."""
521521

522-
uri: str
522+
Annotated[str, UrlConstraints(host_required=False)]
523523
"""The URI of this resource."""
524524
mimeType: str | None = None
525525
"""The MIME type of this resource, if known."""
@@ -569,7 +569,7 @@ class ResourceListChangedNotification(
569569
class SubscribeRequestParams(RequestParams):
570570
"""Parameters for subscribing to a resource."""
571571

572-
uri: str
572+
Annotated[str, UrlConstraints(host_required=False)]
573573
"""
574574
The URI of the resource to subscribe to. The URI can use any protocol; it is up to
575575
the server how to interpret it.
@@ -590,7 +590,7 @@ class SubscribeRequest(Request[SubscribeRequestParams, Literal["resources/subscr
590590
class UnsubscribeRequestParams(RequestParams):
591591
"""Parameters for unsubscribing from a resource."""
592592

593-
uri: str
593+
Annotated[str, UrlConstraints(host_required=False)]
594594
"""The URI of the resource to unsubscribe from."""
595595
model_config = ConfigDict(extra="allow")
596596

@@ -608,7 +608,7 @@ class UnsubscribeRequest(Request[UnsubscribeRequestParams, Literal["resources/un
608608
class ResourceUpdatedNotificationParams(NotificationParams):
609609
"""Parameters for resource update notifications."""
610610

611-
uri: str
611+
Annotated[str, UrlConstraints(host_required=False)]
612612
"""
613613
The URI of the resource that has been updated. This might be a sub-resource of the
614614
one that the client actually subscribed to.
@@ -1084,7 +1084,7 @@ class ResourceTemplateReference(BaseModel):
10841084
"""A reference to a resource or resource template definition."""
10851085

10861086
type: Literal["ref/resource"]
1087-
uri: str
1087+
Annotated[str, UrlConstraints(host_required=False)]
10881088
"""The URI or URI template of the resource."""
10891089
model_config = ConfigDict(extra="allow")
10901090

0 commit comments

Comments
 (0)