Add resource Link (#974)
This commit is contained in:
parent
86bb54cb28
commit
d0443a1832
3 changed files with 66 additions and 22 deletions
|
|
@ -11,7 +11,7 @@ from anyio.abc import TaskStatus
|
|||
from mcp.client.session import ClientSession
|
||||
from mcp.server.lowlevel import Server
|
||||
from mcp.shared.exceptions import McpError
|
||||
from mcp.types import Content, TextContent
|
||||
from mcp.types import ContentBlock, TextContent
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
|
|
@ -31,7 +31,7 @@ async def test_notification_validation_error(tmp_path: Path):
|
|||
slow_request_complete = anyio.Event()
|
||||
|
||||
@server.call_tool()
|
||||
async def slow_tool(name: str, arg) -> Sequence[Content]:
|
||||
async def slow_tool(name: str, arg) -> Sequence[ContentBlock]:
|
||||
nonlocal request_count
|
||||
request_count += 1
|
||||
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@ from mcp.types import (
|
|||
ProgressNotification,
|
||||
PromptReference,
|
||||
ReadResourceResult,
|
||||
ResourceLink,
|
||||
ResourceListChangedNotification,
|
||||
ResourceTemplateReference,
|
||||
SamplingMessage,
|
||||
|
|
@ -152,6 +153,25 @@ def make_everything_fastmcp() -> FastMCP:
|
|||
def echo(message: str) -> str:
|
||||
return f"Echo: {message}"
|
||||
|
||||
# Tool that returns ResourceLinks
|
||||
@mcp.tool(description="Lists files and returns resource links", title="List Files Tool")
|
||||
def list_files() -> list[ResourceLink]:
|
||||
"""Returns a list of resource links for files matching the pattern."""
|
||||
|
||||
# Mock some file resources for testing
|
||||
file_resources = [
|
||||
{
|
||||
"type": "resource_link",
|
||||
"uri": "file:///project/README.md",
|
||||
"name": "README.md",
|
||||
"mimeType": "text/markdown",
|
||||
}
|
||||
]
|
||||
|
||||
result: list[ResourceLink] = [ResourceLink.model_validate(file_json) for file_json in file_resources]
|
||||
|
||||
return result
|
||||
|
||||
# Tool with sampling capability
|
||||
@mcp.tool(description="A tool that uses sampling to generate content", title="Sampling Tool")
|
||||
async def sampling_tool(prompt: str, ctx: Context) -> str:
|
||||
|
|
@ -762,7 +782,17 @@ async def call_all_mcp_features(session: ClientSession, collector: NotificationC
|
|||
assert isinstance(tool_result.content[0], TextContent)
|
||||
assert tool_result.content[0].text == "Echo: hello"
|
||||
|
||||
# 2. Tool with context (logging and progress)
|
||||
# 2. Test tool that returns ResourceLinks
|
||||
list_files_result = await session.call_tool("list_files")
|
||||
assert len(list_files_result.content) == 1
|
||||
|
||||
# Rest should be ResourceLinks
|
||||
content = list_files_result.content[0]
|
||||
assert isinstance(content, ResourceLink)
|
||||
assert str(content.uri).startswith("file:///")
|
||||
assert content.name is not None
|
||||
assert content.mimeType is not None
|
||||
|
||||
# Test progress callback functionality
|
||||
progress_updates = []
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ from mcp.shared.memory import (
|
|||
from mcp.types import (
|
||||
AudioContent,
|
||||
BlobResourceContents,
|
||||
Content,
|
||||
ContentBlock,
|
||||
EmbeddedResource,
|
||||
ImageContent,
|
||||
TextContent,
|
||||
|
|
@ -194,7 +194,7 @@ def image_tool_fn(path: str) -> Image:
|
|||
return Image(path)
|
||||
|
||||
|
||||
def mixed_content_tool_fn() -> list[Content]:
|
||||
def mixed_content_tool_fn() -> list[ContentBlock]:
|
||||
return [
|
||||
TextContent(type="text", text="Hello"),
|
||||
ImageContent(type="image", data="abc", mimeType="image/png"),
|
||||
|
|
|
|||
Loading…
Reference in a new issue