Plugin API¶
This module contains the main plugin class for the Litestar MCP Plugin.
LitestarMCP¶
- class litestar_mcp.plugin.LitestarMCP[source]¶
Bases:
InitPluginProtocol,CLIPluginLitestar plugin for Model Context Protocol integration.
The main plugin class that implements
litestar.plugins.InitPluginProtocol. It discovers routes marked withmcp_tool,mcp_resource, ormcp_promptin theiroptdictionary and exposes them through the MCP Streamable HTTP transport surface. Standalone prompt callables decorated withmcp_prompt()can be passed via thepromptsconstructor argument.- property config: litestar_mcp.config.MCPConfig¶
Get the plugin configuration.
- property discovered_tools: dict[str, BaseRouteHandler]¶
Get discovered MCP tools.
- property discovered_resources: dict[str, BaseRouteHandler]¶
Get discovered MCP resources.
- property discovered_prompts: dict[str, PromptRegistration]¶
Get discovered MCP prompts.
- register_dynamic_handler(handler)[source]¶
Register a dynamic route handler on the plugin.
This is typically used by the wrapper class to register decorated tools and resources.
- Return type:
- on_app_init(app_config)[source]¶
Initialize the MCP integration when the Litestar app starts.
- Return type:
MCP¶
- class litestar_mcp.app.MCP[source]¶
Bases:
objectA class that simplifies Model Context Protocol application setup.
This provides decorators and programmatic server execution for standalone applications.
The standalone application wrapper. Builds a Litestar app pre-configured with
LitestarMCP, exposes@tool/@resource/@promptdecorators, and can serve over Streamable HTTP or stdio viaMCP.run(). Pass aMCPStdioContexttorun(transport="stdio", stdio_context=...)to seed the caller identity.- __init__(name, *, instructions=None, config=None, plugins=None, route_handlers=None, **kwargs)[source]¶
- property app: Litestar¶
Get the Litestar application instance.
This lazily instantiates the Litestar app upon first access, ensuring all dynamically registered handlers are captured.
- tool(name=None, *, after_request=None, after_response=None, background=None, before_request=None, cache=False, cache_control=None, cache_key_builder=None, dependencies=None, dto=_EmptyEnum.EMPTY, etag=None, exception_handlers=None, guards=None, media_type=None, middleware=None, route_name=None, opt=None, request_class=None, response_class=None, response_cookies=None, response_headers=None, return_dto=_EmptyEnum.EMPTY, signature_namespace=None, status_code=None, sync_to_thread=None, content_encoding=None, content_media_type=None, deprecated=False, description=None, include_in_schema=_EmptyEnum.EMPTY, operation_class=<class 'litestar.openapi.spec.operation.Operation'>, operation_id=None, raises=None, response_description=None, responses=None, security=None, summary=None, tags=None, type_decoders=None, type_encoders=None, **kwargs)[source]¶
Decorator to register a function as an MCP tool.
This dynamically wraps the function inside a Litestar route handler and registers it to the plugin. Additional keyword arguments are passed through to Litestar's
post()route decorator.
- resource(uri, *, name=None, mime_type=None, after_request=None, after_response=None, background=None, before_request=None, cache=False, cache_control=None, cache_key_builder=None, dependencies=None, dto=_EmptyEnum.EMPTY, etag=None, exception_handlers=None, guards=None, media_type=None, middleware=None, route_name=None, opt=None, request_class=None, response_class=None, response_cookies=None, response_headers=None, return_dto=_EmptyEnum.EMPTY, signature_namespace=None, status_code=None, sync_to_thread=None, content_encoding=None, content_media_type=None, deprecated=False, description=None, include_in_schema=_EmptyEnum.EMPTY, operation_class=<class 'litestar.openapi.spec.operation.Operation'>, operation_id=None, raises=None, response_description=None, responses=None, security=None, summary=None, tags=None, type_decoders=None, type_encoders=None, **kwargs)[source]¶
Decorator to register a function as an MCP resource.
This dynamically wraps the function inside a Litestar route handler and registers it to the plugin. Additional keyword arguments are passed through to Litestar's
get()route decorator.
- prompt(name=None, *, after_request=None, after_response=None, background=None, before_request=None, cache=False, cache_control=None, cache_key_builder=None, dependencies=None, dto=_EmptyEnum.EMPTY, etag=None, exception_handlers=None, guards=None, media_type=None, middleware=None, route_name=None, opt=None, request_class=None, response_class=None, response_cookies=None, response_headers=None, return_dto=_EmptyEnum.EMPTY, signature_namespace=None, status_code=None, sync_to_thread=None, content_encoding=None, content_media_type=None, deprecated=False, description=None, include_in_schema=_EmptyEnum.EMPTY, operation_class=<class 'litestar.openapi.spec.operation.Operation'>, operation_id=None, raises=None, response_description=None, responses=None, security=None, summary=None, tags=None, type_decoders=None, type_encoders=None, **kwargs)[source]¶
Decorator to register a function as an MCP prompt.
This dynamically wraps the function inside a Litestar route handler and registers it to the plugin. Additional keyword arguments are passed through to Litestar's
get()route decorator.
Registry¶
- class litestar_mcp.registry.Registry[source]¶
Bases:
objectCentral registry for MCP tools, resources, and prompts.
This class decouples metadata storage and discovery from the route handlers themselves, avoiding issues with __slots__ or object mutation.
Note
Tools and resources are stored as bare
BaseRouteHandlervalues because every entry has a single underlying handler. Prompts usePromptRegistrationinstead — a prompt may originate from either a standalone@mcp_promptcallable or a route handler, so the dataclass carries thefn | handlerunion plus the per-prompt metadata (title, description, arguments, icons) that can't live on a bare callable.- register_change_callback(callback)[source]¶
Register a callback to be invoked when the registry changes.
- Parameters:
callback¶ -- The callback function.
- unregister_change_callback(callback)[source]¶
Unregister a change callback.
- Parameters:
callback¶ -- The callback function to remove.
- set_subscription_manager(manager)[source]¶
Set the subscription manager for notifications.
- Return type:
- property subscription_manager: SubscriptionManager¶
Return the configured subscription manager.
- property tools: dict[str, BaseRouteHandler]¶
Get registered tools.
- property resources: dict[str, BaseRouteHandler]¶
Get registered resources.
- register_tool(name, handler)[source]¶
Register a tool.
- Parameters:
handler¶ (
BaseRouteHandler) -- The route handler.
- Return type:
- register_resource(name, handler)[source]¶
Register a resource.
- Parameters:
handler¶ (
BaseRouteHandler) -- The route handler.
- Return type:
- property templates: dict[str, ResourceTemplate]¶
Get registered resource templates, keyed by resource name.
- register_resource_template(name, handler, template)[source]¶
Register an RFC 6570 Level 1 URI template for a resource.
- Parameters:
name¶ (
str) -- The resource name (same key asregister_resource).handler¶ (
BaseRouteHandler) -- The route handler bound to the template.template¶ (
str) -- The URI template string. Validated at registration; invalid templates raiseValueError.
- Return type:
- property prompts: dict[str, PromptRegistration]¶
Get registered prompts.
- register_prompt(name, fn, *, title=None, description=None, arguments=None, icons=None)[source]¶
Register a standalone prompt function.
- Parameters:
name¶ -- Unique prompt identifier.
fn¶ -- The callable to invoke on
prompts/get.title¶ -- Optional human-readable display name.
description¶ -- Optional description. Falls back to
fn.__doc__.arguments¶ -- Explicit argument definitions. When
None, derived from the function signature.icons¶ -- Optional list of icon objects for UI display.
- register_prompt_handler(name, handler, *, title=None, description=None, arguments=None, icons=None)[source]¶
Register a route-handler-based prompt.
Storage only — runtime dispatch and the
messages-passthrough vs. normalize-on-return decision live inlitestar_mcp.routes.handle_prompts_get(). This function captures the handler reference plus any explicit overrides so the registry can renderprompts/listentries without executing the handler.- Parameters:
handler¶ (
BaseRouteHandler) -- The Litestar route handler.title¶ (
Optional[str]) -- Optional human-readable display name.arguments¶ (
Optional[list[dict[str, typing.Any]]]) -- Explicit argument definitions. WhenNone, arguments are introspected from the handler's parsed handler signature atprompts/listrender time (DI- and framework-injected parameters filtered out). Pass[]to advertise no arguments explicitly.icons¶ (
Optional[list[dict[str, typing.Any]]]) -- Optional list of icon objects for UI display.
- Return type:
- async publish_notification(method, params)[source]¶
Publish a JSON-RPC 2.0 notification to connected clients.