brainwires-mcp-server
MCP server framework with composable middleware for the Brainwires Agent Framework.
Overview
brainwires-mcp-server provides everything needed to build an MCP-compliant tool server:
McpServer— Async event loop that reads JSON-RPC requests, runs the middleware chain, and dispatches to your handlerMcpHandler— Trait defining your server's identity, capabilities, and tool dispatchMcpToolRegistry— Declarative tool registration with automatic dispatch by tool nameMiddlewareChain— Composable onion-model middleware pipeline- Built-in middlewares — Auth, logging, rate limiting, and tool filtering included
This crate was extracted from brainwires-agent-network so that consumers who only need to build MCP servers don't have to pull in the full networking stack.
JSON-RPC request
│
▼
McpServer::run()
│
▼
┌─────────────────────────────────────────┐
│ MiddlewareChain │
│ AuthMiddleware → LoggingMiddleware → │
│ RateLimitMiddleware → ToolFilter │
└──────────────────────┬──────────────────┘
│
▼
McpHandler::call_tool()
(or list_tools / initialize)
Quick Start
[]
= "0.7"
Minimal server:
use ;
use ;
use async_trait;
use Value;
;
async
With Auth and Rate Limiting
use ;
let server = new
.with_transport
.with_middleware
.with_middleware // 100 req/min
.with_middleware; // block bash tool
server.run.await?;
Using McpToolRegistry
McpToolRegistry handles dispatch automatically — register handlers and call dispatch():
use ;
use CallToolResult;
use Value;
let mut registry = new;
registry.register;
// In your McpHandler::call_tool():
// registry.dispatch(tool_name, args).await
API Reference
Core Types
| Type | Description |
|---|---|
McpServer<H> |
Server lifecycle — wires handler, middleware chain, and transport |
McpHandler |
Trait: server_info(), capabilities(), list_tools(), call_tool() |
McpToolRegistry |
Declarative registry with register() and dispatch() |
McpToolDef |
Tool definition (name, description, input schema) |
ToolHandler |
Boxed async fn (Value) -> Result<CallToolResult> |
ServerTransport |
Trait for request/response I/O |
StdioServerTransport |
Stdio transport (reads from stdin, writes to stdout) |
MiddlewareChain |
Ordered middleware list; processed in insertion order |
Middleware |
Trait: handle(request, next) -> MiddlewareResult |
MiddlewareResult |
Continue or short-circuit with a response |
RequestContext |
Per-request info: client ID, remote address, auth token |
ClientInfo |
Client identity attached to the request context |
Middleware
| Type | Description |
|---|---|
AuthMiddleware |
Bearer token validation; rejects with JSON-RPC -32001 on failure |
LoggingMiddleware |
Structured tracing spans for every request |
RateLimitMiddleware |
Token-bucket rate limiter; rejects with -32029 when over budget |
ToolFilterMiddleware |
Allow-list or deny-list; rejects denied tools with -32004 |
Integration
Use via the brainwires facade crate:
[]
= { = "0.7", = ["mcp-server-framework"] }
Or use standalone — brainwires-mcp-server depends only on brainwires-mcp.
License
Licensed under either of Apache License, Version 2.0 or MIT License at your option.