-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Feature: MCP client Resources support #3024
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 29 commits
9fde764
a7404a2
ecea466
1886299
dfb54db
f52bb71
232ff04
38675da
317416c
67f9b07
e6cb086
b8424d8
50cab26
6857ef2
18743cd
4e5d627
6f3a87a
72d66ac
3000511
30f4e7f
456e714
3d95521
918f85c
28cf761
9804f18
d173f88
f7c5319
21ad2b0
14c3973
44c327a
7346061
b9216b4
3d47349
278d141
2445540
eb31106
196d139
94f91ec
509d6ef
a9e10cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import base64 | ||
| from collections.abc import Sequence | ||
| from typing import Literal | ||
| from typing import TYPE_CHECKING, Literal | ||
|
|
||
| from . import exceptions, messages | ||
|
|
||
|
|
@@ -12,6 +12,9 @@ | |
| 'you can use the `mcp` optional group — `pip install "pydantic-ai-slim[mcp]"`' | ||
| ) from _import_error | ||
|
|
||
| if TYPE_CHECKING: | ||
| from .mcp import Resource, ResourceTemplate, ServerCapabilities | ||
|
Comment on lines
+17
to
+18
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it makes more sense to define those here. There's a weird local import on most of the functions in this file.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually... Do the methods here need to be functions here? Since they are just transformations, can't they live in the dataclasses itself? e.g. instead of
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, totally. I originally implemented them like so mostly as that was (roughly) the pattern that already existed in that file but I actually prefer |
||
|
|
||
|
|
||
| def map_from_mcp_params(params: mcp_types.CreateMessageRequestParams) -> list[messages.ModelMessage]: | ||
| """Convert from MCP create message request parameters to pydantic-ai messages.""" | ||
|
|
@@ -121,3 +124,56 @@ def map_from_sampling_content( | |
| return messages.TextPart(content=content.text) | ||
| else: | ||
| raise NotImplementedError('Image and Audio responses in sampling are not yet supported') | ||
|
|
||
|
|
||
| def map_from_mcp_resource(mcp_resource: mcp_types.Resource) -> 'Resource': | ||
| """Convert from MCP Resource to native Pydantic AI Resource.""" | ||
| from .mcp import Resource, ResourceAnnotations | ||
|
|
||
| return Resource( | ||
| uri=str(mcp_resource.uri), | ||
| name=mcp_resource.name, | ||
| title=mcp_resource.title, | ||
| description=mcp_resource.description, | ||
| mime_type=mcp_resource.mimeType, | ||
| size=mcp_resource.size, | ||
| annotations=( | ||
fennb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ResourceAnnotations(audience=mcp_resource.annotations.audience, priority=mcp_resource.annotations.priority) | ||
| if mcp_resource.annotations | ||
| else None | ||
| ), | ||
| metadata=mcp_resource.meta, | ||
| ) | ||
|
|
||
|
|
||
| def map_from_mcp_resource_template(mcp_template: mcp_types.ResourceTemplate) -> 'ResourceTemplate': | ||
| """Convert from MCP ResourceTemplate to native Pydantic AI ResourceTemplate.""" | ||
| from .mcp import ResourceAnnotations, ResourceTemplate | ||
|
|
||
| return ResourceTemplate( | ||
| uri_template=mcp_template.uriTemplate, | ||
| name=mcp_template.name, | ||
| title=mcp_template.title, | ||
| description=mcp_template.description, | ||
| mime_type=mcp_template.mimeType, | ||
| annotations=( | ||
| ResourceAnnotations(audience=mcp_template.annotations.audience, priority=mcp_template.annotations.priority) | ||
| if mcp_template.annotations | ||
| else None | ||
| ), | ||
| metadata=mcp_template.meta, | ||
| ) | ||
|
|
||
|
|
||
| def map_from_mcp_server_capabilities(mcp_capabilities: mcp_types.ServerCapabilities) -> 'ServerCapabilities': | ||
fennb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """Convert from MCP ServerCapabilities to native Pydantic AI ServerCapabilities.""" | ||
| from .mcp import ServerCapabilities | ||
|
|
||
| return ServerCapabilities( | ||
| experimental=list(mcp_capabilities.experimental.keys()) if mcp_capabilities.experimental else None, | ||
| logging=mcp_capabilities.logging is not None, | ||
| prompts=mcp_capabilities.prompts is not None, | ||
| resources=mcp_capabilities.resources is not None, | ||
| tools=mcp_capabilities.tools is not None, | ||
| completions=mcp_capabilities.completions is not None, | ||
| ) | ||
Uh oh!
There was an error while loading. Please reload this page.