feat(adapters): from_mcp + to_mcp behind the mcp feature (rmcp 1.8)

from_mcp (src/adapters/from_mcp/):
- tools/list discovery over streamable HTTP; per-tool
  HandlerRegistration (Mutation, Once, FromMCP leaf, Internal;
  ADR-015/022)
- structuredContent-preferred output, ContentBlock-union fallback,
  isError -> MCP_TOOL_ERROR with content blocks as details (ADR-023)
- bearer token flows via capabilities key 'mcp' (ADR-014 no-env-vars)
- 19 unit tests + tests/from_mcp_integration.rs (5 tests vs a real
  rmcp streamable-HTTP MCP server)

to_mcp (src/adapters/to_mcp.rs):
- 4 fixed gateway tools (search/schema/call/batch, ADR-041); Sub ops
  excluded from search and uncallable (MCP is request/response)
- identity survives rmcp framing: bearer_auth_middleware stashes
  Option<Identity> in http::request::Parts extensions, call_tool reads
  it back from RequestContext extensions
- StreamableHttpService nested at /mcp in HttpAdapter's router,
  bearer middleware around it (feature-gated)

Streamable HTTP only (ADR-037): rmcp default-features off, no stdio.
Default build compiles without rmcp (cargo tree: 0 hits).

Verified: cargo test (182 lib default / 218 all-features) + 5 MCP
integration + 10 WS, clippy -D warnings (both), fmt.
This commit is contained in:
2026-08-28 14:14:09 +00:00
parent 7be91987ca
commit 4ac337c3a5
8 changed files with 1915 additions and 10 deletions
+66 -1
View File
@@ -132,6 +132,25 @@ impl HttpAdapter {
fn build_router(state: RouterState, extra_routes: Option<Router>) -> Router {
let auth_state = Arc::clone(&state.identity_provider);
#[cfg(feature = "mcp")]
let mcp_router: Router<RouterState> = {
let dispatch = crate::gateway::GatewayDispatch::new(
Arc::clone(&state.registry),
Arc::clone(&state.identity_provider),
);
Router::new()
.nest_service(
"/mcp",
crate::adapters::to_mcp_service(std::sync::Arc::new(dispatch)),
)
.layer(from_fn_with_state(
auth_state.clone(),
bearer_auth_middleware,
))
};
#[cfg(not(feature = "mcp"))]
let mcp_router: Router<RouterState> = Router::new();
let default: Router<RouterState> = Router::new()
.merge(crate::gateway::routes::gateway_router())
.route("/openapi.json", get(openapi_json_handler))
@@ -140,7 +159,8 @@ fn build_router(state: RouterState, extra_routes: Option<Router>) -> Router {
auth_state.clone(),
bearer_auth_middleware,
))
.fallback(decoy_fallback);
.fallback(decoy_fallback)
.merge(mcp_router);
let with_extras = match extra_routes {
Some(extra) => {
@@ -442,6 +462,51 @@ mod tests {
assert!(text.contains("1.1.0"), "info.version 1.1.0 in doc");
assert!(text.contains("gatewayPublish"), "publish operationId");
let _ = server_task.await;
}
#[cfg(feature = "mcp")]
#[tokio::test]
async fn mcp_endpoint_serves_four_gateway_tools_bearer_gated() {
use tokio::io::{AsyncReadExt, AsyncWriteExt};
let adapter = HttpAdapter::new(provider(), empty_registry());
let (client, server) = tokio::io::duplex(256 * 1024);
let conn = Connection::from_bidi(server, b"http/1.1".to_vec(), None);
let auth = AuthContext::anonymous(b"http/1.1");
let server_task = tokio::spawn(async move {
let _ = ProtocolHandler::handle(&adapter, conn, &auth).await;
});
let mut client = client;
client
.write_all(
b"POST /mcp HTTP/1.1\r\nHost: localhost\r\nContent-Type: application/json\r\nAccept: application/json, text/event-stream\r\nContent-Length: 175\r\nConnection: close\r\n\r\n{\"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"initialize\", \"params\": {\"protocolVersion\": \"2025-06-18\", \"capabilities\": {}, \"clientInfo\": {\"name\": \"test-client\", \"version\": \"1.0.0\"}}}",
)
.await
.unwrap();
let mut response = Vec::new();
let _ = tokio::time::timeout(
std::time::Duration::from_secs(5),
client.read_to_end(&mut response),
)
.await
.expect("read timed out")
.unwrap();
let text = String::from_utf8_lossy(&response);
// Bearer middleware is applied around the nested service: no token
// means no identity stash, but the middleware does not enforce —
// the MCP initialize response must still come back.
assert!(
text.starts_with("HTTP/1.1 200 OK"),
"initialize over /mcp got: {text}"
);
assert!(
text.contains("alkhttp-to-mcp"),
"server info in initialize response: {text}"
);
let _ = server_task.await;
}
}