Skip to main content

Crate klieo_mcp_server

Crate klieo_mcp_server 

Source
Expand description

Expose any klieo ToolInvoker as an MCP server over stdio or HTTP (see the http cargo feature).

This crate is the inverse of klieo-tools-mcp: instead of a Rust host calling external MCP servers, it lets external MCP hosts (Claude Desktop, Continue, LangGraph, OpenAI Agents SDK) call into a Rust-built klieo ToolInvoker.

Polyglot strategy: klieo exposes three wire-level contracts so non-Rust hosts can integrate without depending on klieo crates — the NATS bus (durable pipelines), this MCP server (stdio/HTTP tool calls), and the A2A peer protocol (JSON-RPC). This crate covers the MCP-server contract.

§Scope

  • stdio transport — newline-delimited JSON-RPC 2.0 frames on stdin/stdout, matching the MCP reference spec.
  • tools/list — derived from the invoker’s catalogue.
  • tools/call — dispatches to ToolInvoker::invoke with a ToolCtx minted per request from the McpServerBuilder::with_tool_ctx_factory-supplied factory. Default factory is the in-memory noop wiring (Pubsub/KvStore/ JobQueue from klieo-bus-memory, fresh per call). Override the factory to expose tools that need real bus access.
  • initialize / shutdown — standard MCP handshake.

Out-of-scope:

  • SSE streaming responses (HTTP transport returns JSON only).
  • Multi-tenant auth (wrap router() with a tower auth layer, or front with an auth-enforcing reverse proxy).

§Agent exposure (0.9, ADR-010)

McpServer::expose_agent_with_schema(agent, schema, ctx_factory) wraps any klieo_core::Agent as a single MCP tool whose inputSchema is the caller-supplied JSON Schema. The ctx_factory closure is called per tools/call to mint a fresh klieo_core::agent::AgentContext (so each invocation gets its own RunId).

Behind the schemars cargo feature, expose_agent::<A>(agent, ctx_factory) derives the schema automatically via schema_for!(A::Input). See ADR-010 for the trade-off.

Re-exports§

pub use outbound_sink::OutboundFrameSink;
pub use outbound_sink::OutboundSinkError;
pub use sampling::ModelHint;
pub use sampling::ModelPreferences;
pub use sampling::SamplingContent;
pub use sampling::SamplingMessage;
pub use sampling::SamplingRequest;
pub use sampling::SamplingResponse;
pub use roots::Root;
pub use outbound_ext::McpOutboundExt;
pub use outbound_sink::bench_stdio_sink;

Modules§

outbound_ext
Typed extension methods over klieo_core::ServerOutbound.
outbound_sink
Per-transport frame-write abstraction over JSON-RPC envelopes.
roots
MCP roots — client-declared URI scopes the server may operate on.
sampling
MCP sampling/createMessage wire-format types.

Structs§

McpServer
MCP server that exposes a klieo-core ToolInvoker as a stdio MCP server.
McpServerBuilder
Builder for McpServer. Collects one or more invokers (raw ToolInvokers or Agents wrapped as single-tool invokers) and configures server-level concerns such as transports, parent cancel, leader election, tenant binding, and authentication.

Enums§

McpBuildError
Errors returned by McpServerBuilder::build and McpServerBuilder::build_arc.
McpServerError
Top-level error from the MCP server loop.
OrphanOutcome
Re-export the cluster-0.24 follower-side orphan re-invoke gate for integration tests. Gated on test-fixtures so production callers cannot reach the helper. See crate::http::OrphanOutcome

Constants§

LEADER_TTL
Leader-claim TTL for the klieo-leaders KV bucket used by stream_tools_call (claim on invoke) and stream_resume (orphan check on resume). Operators MUST configure the JetStream KV bucket with max_age equal to this value so a dead replica’s leader entry evicts automatically; the registry’s heartbeat runs every TTL / 2. See ADR-020.
MCP_LEADER_KEY_PREFIX
Leader-key prefix for the MCP transport in the shared klieo-leaders bucket. Mirrors a2a.<task_id> on the A2A side.
MCP_PROTOCOL_VERSION
MCP protocol revision advertised on every initialize response.

Functions§

bench_filter_replay
Scans a replay-buffer snapshot and returns entries with event_id > since_id. Approximates the per-frame scan cost of the SSE replay path without HTTP header parsing or lock acquisition overhead. Available only under the bench feature.
encode_sse_frame
Encodes one SSE frame into a Bytes buffer: id: <N>\ndata: <json>\n\n.
handle_dead_leader_orphan_mcp
Re-export the cluster-0.24 follower-side orphan re-invoke gate for integration tests. Gated on test-fixtures so production callers cannot reach the helper. See crate::http::OrphanOutcome

Type Aliases§

AgentContextFactory
Factory for fresh klieo_core::agent::AgentContext values, called once per tools/call so each invocation gets its own RunId + cancel token. Caller-owned to keep klieo-mcp-server free of opinions about which memory / bus / llm backends an agent runs against.
ToolCtxFactory
Factory for fresh klieo_core::tool::ToolCtx values, called once per tools/call so each invocation gets its own Pubsub / KvStore / JobQueue (or a shared one, caller’s choice). Default = default_tool_ctx_factory which mints the same per-request in-memory wiring serve_stdio has used since 0.8. Override via McpServerBuilder::with_tool_ctx_factory to expose bus-using tools through this server.