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 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.
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.- property sse_manager: SSEManager¶
Return the configured SSE 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:
fn¶ (
Callable[...,Any]) -- The callable to invoke onprompts/get.title¶ (
Optional[str]) -- Optional human-readable display name.description¶ (
Optional[str]) -- Optional description. Falls back tofn.__doc__.arguments¶ (
Optional[list[dict[str,Any]]]) -- Explicit argument definitions. WhenNone, derived from the function signature.icons¶ (
Optional[list[dict[str,Any]]]) -- Optional list of icon objects for UI display.
- Return type:
- 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,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,Any]]]) -- Optional list of icon objects for UI display.
- Return type:
- async publish_notification(method, params, session_id=None)[source]¶
Publish a JSON-RPC 2.0 notification to connected clients.
SSEManager¶
- class litestar_mcp.sse.SSEManager[source]¶
Bases:
objectManages Streamable HTTP SSE connections and message delivery.
The manager keeps one
_StreamStateper open stream and a side index fromsession_idto the set of stream ids currently attached to that session, so notifications can be fanned out to every stream belonging to a given MCP session.- async open_stream(session_id=None, last_event_id=None)[source]¶
Open a new stream (or resume from
last_event_id).- Parameters:
session_id¶ (
Optional[str]) -- Optional session id to bind this stream to. When provided, the manager updates the session→streams index sopublish()can fan out to all streams belonging to the session.last_event_id¶ (
Optional[str]) -- OptionalLast-Event-IDvalue. On a match, pending messages from the existing stream's history are replayed before normal delivery resumes.
- Return type:
- Returns:
A
(stream_id, async_generator)pair.
- async enqueue(stream_id, message)[source]¶
Enqueue a raw JSON payload onto a single stream.
- Return type:
- async publish(message, session_id=None)[source]¶
Publish a JSON payload to one or all sessions.
When
session_idis provided the message fans out to every stream attached to that session; otherwise it fans out to every stream attached to any session.- Return type: