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_toolormcp_resourcein theiroptdictionary and exposes them through the MCP Streamable HTTP transport surface.- property discovered_tools: dict[str, BaseRouteHandler]¶
Get discovered MCP tools.
- property discovered_resources: dict[str, BaseRouteHandler]¶
Get discovered MCP resources.
Registry¶
- class litestar_mcp.registry.Registry[source]¶
Bases:
objectCentral registry for MCP tools and resources.
This class decouples metadata storage and discovery from the route handlers themselves, avoiding issues with __slots__ or object mutation.
- 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:
- 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: