Skip to content

Commit

Permalink
Add handler for resource templates
Browse files Browse the repository at this point in the history
We missed to have a handler for resource templates. We add this
now. One caveat is still that the `uriTemplate` in `ResourceTemplate`
is not a pydantic type.
  • Loading branch information
dsp-ant committed Nov 27, 2024
1 parent ab2f502 commit 587dbb7
Showing 1 changed file with 19 additions and 0 deletions.
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

0 comments on commit 587dbb7

Please sign in to comment.