McpServerPlugin class

Runtime ZuraffaPlugin that exposes the host app's McpTools over the Model Context Protocol.

What it does

During the engine's bootstrap phase, registerDependencies populates a McpToolRegistry (registered as a singleton in the DI container) with every tool passed to the plugin's constructor. onInit then captures the ZuraffaDIContainer for later use by serveStdio / serveSse / listTools.

Usage (typical generated app)

Generated by zfa plugin mcp, the host lib/main.dart becomes:

import 'package:zuraffa/zuraffa.dart';
import 'package:my_app/src/plugin/mcp_server_plugin.dart';
import 'package:my_app/src/mcp/tools.dart';

void main() async {
  final engine = ZuraffaEngine()
    ..register(McpServerPlugin(tools: myMcpTools));
  await engine.bootstrap();

  // Run the stdio MCP server. Blocks forever.
  await engine['mcp']!.serveStdio();
}

Design notes

  • The plugin does NOT auto-start any server by default. The host app decides when to start the loop (typically at the end of main() after engine.bootstrap() completes). Pass autoStartStdio: true to start the loop in onInit.
  • The routes getter returns an empty map — the MCP plugin does not contribute to the host app's route table.
  • pluginId defaults to 'mcp'; pass a custom id if you need multiple MCP server plugins in the same engine (rare).
Inheritance

Constructors

McpServerPlugin({String pluginId = 'mcp', List<McpTool>? tools, bool autoStartStdio = false, int? autoStartSsePort, String? sseAuthToken, int? maxSseConnections, Map<String, Set<String>>? sseToolAllowlist, int? autoStartHttpPort, String? httpAuthToken, Map<String, Set<String>>? httpToolAllowlist, bool httpBindAll = false})

Properties

autoStartHttpPort → int?
If non-null, onInit automatically starts the Streamable HTTP server (issue #2046) on the given port. Pair with httpAuthToken for anything beyond loopback consumption.
final
autoStartSsePort → int?
If non-null, onInit automatically starts the SSE server on the given port. Pair with sseAuthToken for remote agents.
final
autoStartStdio → bool
If true, onInit automatically starts the stdio server. Useful for bin-only apps that exist solely to serve MCP.
final
hashCode → int
The hash code for this object.
no setterinherited
httpAuthToken → String?
Bearer token for the Streamable HTTP server. Forwarded to McpStreamableHttpServer for both the auto-started and explicit serveHttp servers. Required when the HTTP server binds non-loopback.
final
httpBindAll → bool
When true, the Streamable HTTP server binds 0.0.0.0 instead of the loopback default. Refused without httpAuthToken.
final
httpServer → McpStreamableHttpServer?
The McpStreamableHttpServer started by serveHttp, or null if none has been started (or after it has been stopped). Use McpStreamableHttpServer.isRunning / McpStreamableHttpServer.stop on the returned instance.
no setter
httpToolAllowlist → Map<String, Set<String>>?
Per-token tool allowlists forwarded to the Streamable HTTP server (same shape as sseToolAllowlist).
final
maxSseConnections → int?
Maximum number of concurrent SSE sessions (forwarded to McpSseServer). null (default) means unbounded. Requirement #3.
final
pluginId → String
Unique identifier for this plugin.
final
registry → McpToolRegistry
Returns the live McpToolRegistry from the DI container.
no setter
routes → Map<String, ZuraffaRouteHandler>
Route table contributed by this plugin.
no setteroverride
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
sseAuthToken → String?
Bearer token for the auto-started SSE server. Ignored unless autoStartSsePort is set.
final
sseServer → McpSseServer?
The McpSseServer started by serveSse, or null if none has been started. This reference is retained even after the server is stopped or if serveSse fails during startup. Use McpSseServer.isRunning to check whether the server is currently running. Callers and tests can invoke stop() on the returned instance after await plugin.serveSse(...) returns.
no setter
sseToolAllowlist → Map<String, Set<String>>?
Per-token tool allowlists forwarded to McpSseServer (requirement #3). A token maps to the set of tool names it may invoke; anonymous requests are denied tool calls when any allowlist is configured.
final
tools → List<McpTool>
The tools this plugin exposes. Held immutably — the McpToolRegistry in DI is the live source of truth after bootstrap.
final

Methods

listTools() → List<McpTool>
Lists the tools currently registered. Pre-bootstrap, returns the static tools list; post-bootstrap, returns the live registry contents (which may differ if the host app added tools via registry.register(...) after bootstrap).
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onDispose(ZuraffaDIContainer di) → Future<void>
Disposal hook called by ZuraffaEngine.shutdown in reverse registration order (spec 025 — package runtime modules).
override
onInit(ZuraffaDIContainer di) → Future<void>
Asynchronous initialisation hook called after all plugins have completed registerDependencies.
override
onReady(ZuraffaDIContainer di) → Future<void>
Readiness hook called by ZuraffaEngine.ready after bootstrap has completed for every plugin (spec 025 — package runtime modules).
inherited
registerDependencies(ZuraffaDIContainer di) → void
Registers this plugin's dependencies into the shared di container.
override
serveHttp({int port = 8373, String? authToken, bool bindAll = false, String serverName = 'zuraffa-app-mcp', String serverVersion = '1.0.0'}) → Future<void>
Starts the Streamable HTTP MCP server (issue #2046) on port. Returns when the server is bound (not when it shuts down).
serveSse({int port = 8372, String? authToken, String serverName = 'zuraffa-app-mcp', String serverVersion = '1.0.0'}) → Future<void>
Starts an SSE MCP server on port. Returns when the server is bound (not when it shuts down).
serveStdio({String serverName = 'zuraffa-app-mcp', String serverVersion = '1.0.0'}) → Future<void>
Starts the stdio MCP server. Blocks forever in production (stdin is held open); in tests with an injected stream it returns when the stream ends.
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited