Skip to content
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

Add handler for resource templates #77

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/mcp/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ async def handle_call_tool(
) -> list[types.TextContent | types.ImageContent | types.EmbeddedResource]:
# Implementation

@server.list_resource_templates()
async def handle_list_resource_templates() -> list[types.ResourceTemplate]:
# Implementation

3. Define notification handlers if needed:
@server.progress_notification()
async def handle_progress(
Expand Down Expand Up @@ -227,6 +231,21 @@ async def handler(_: Any):

return decorator

def list_resource_templates(self):
def decorator(func: Callable[[], Awaitable[list[types.ResourceTemplate]]]):
logger.debug("Registering handler for ListResourceTemplatesRequest")

async def handler(_: Any):
templates = await func()
return types.ServerResult(
types.ListResourceTemplatesResult(resourceTemplates=templates)
)

self.request_handlers[types.ListResourceTemplatesRequest] = handler
return func

return decorator

def read_resource(self):
def decorator(func: Callable[[AnyUrl], Awaitable[str | bytes]]):
logger.debug("Registering handler for ReadResourceRequest")
Expand Down