Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions infra/lib/api-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,35 @@ export class ApiStack extends cdk.Stack {
integration: lambdaIntegration,
});

// CoderHelm's MCP server (Streamable HTTP, POST; GET and DELETE answer
// 405) and how MCP clients sign in to it: the metadata documents and the
// OAuth endpoints (gateway mcp_server.rs, routes/mcp_oauth.rs). OPTIONS
// too, for browser-based MCP clients: the gateway answers preflights.
httpApi.addRoutes({
path: "/mcp",
methods: [
apigatewayv2.HttpMethod.GET,
apigatewayv2.HttpMethod.POST,
apigatewayv2.HttpMethod.DELETE,
apigatewayv2.HttpMethod.OPTIONS,
],
integration: lambdaIntegration,
});
httpApi.addRoutes({
path: "/.well-known/{proxy+}",
methods: [apigatewayv2.HttpMethod.GET, apigatewayv2.HttpMethod.OPTIONS],
integration: lambdaIntegration,
});
httpApi.addRoutes({
path: "/oauth/{proxy+}",
methods: [
apigatewayv2.HttpMethod.GET,
apigatewayv2.HttpMethod.POST,
apigatewayv2.HttpMethod.OPTIONS,
],
integration: lambdaIntegration,
});

// Auth routes
httpApi.addRoutes({
path: "/auth/{proxy+}",
Expand Down
7 changes: 6 additions & 1 deletion infra/lib/web-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* service paths to their backends, all on one hostname:
*
* /api/plans/chat/stream /api/brain/stream → the streaming function URL (SSE)
* /api/* /auth/* /webhooks/* /email/* /integrations/* → the HTTP API
* /api/* /auth/* /webhooks/* /email/* /integrations/* /mcp /oauth/* → the HTTP API
* everything else → the dashboard (static export in S3)
*
* One hostname means the session cookie (`__Host-`, SameSite=Strict) is
Expand Down Expand Up @@ -45,6 +45,11 @@ export const API_PATHS = [
"/agent-approvals/*",
"/plugins/approve*",
"/openapi.json",
// The MCP server and how its clients sign in (the dashboard's consent
// page is /authorize, outside these).
"/mcp",
"/.well-known/oauth-*",
"/oauth/*",
];
/** The chat stream (SSE, served by the streaming function). */
export const STREAM_PATH = "/api/plans/chat/stream";
Expand Down
28 changes: 28 additions & 0 deletions infra/test/synth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,34 @@ test("single origin: every public route the gateway serves outside /api reaches
assert.ok(covered("/agent-approvals/link"));
});

test("the MCP server and its sign-in reach the gateway, hosted and self-hosted", () => {
const cfg = loadConfigFile(path.join(INFRA, "config", "hosted.prod.json"), { CDK_DEFAULT_ACCOUNT: "654654210434" });
const api = synth(cfg, zoneContext("654654210434", "us-east-1", "coderhelm.com"))["coderhelm-prod-api"];
const keys = Object.values(api.findResources("AWS::ApiGatewayV2::Route")).map((r: any) => String(r.Properties.RouteKey));
for (const [m, p] of [
["POST", "/mcp"],
["GET", "/mcp"],
["OPTIONS", "/mcp"],
["GET", "/.well-known/oauth-protected-resource/mcp"],
["GET", "/.well-known/oauth-authorization-server"],
["POST", "/oauth/register"],
["GET", "/oauth/authorize"],
["POST", "/oauth/token"],
["POST", "/oauth/revoke"],
]) {
assert.ok(keys.some((k) => routeTakes(k, m, p)), `no HTTP API route for ${m} ${p}`);
}
// Single origin: CloudFront sends them to the API, and the dashboard's
// consent page (/authorize) to the dashboard.
const covered = (p: string) =>
API_PATHS.some((a) => (a.endsWith("*") ? p.startsWith(a.slice(0, -1)) : p === a));
for (const p of ["/mcp", "/.well-known/oauth-protected-resource", "/.well-known/oauth-protected-resource/mcp", "/.well-known/oauth-authorization-server", "/oauth/token"]) {
assert.ok(covered(p), `${p} isn't in API_PATHS`);
}
assert.ok(!covered("/authorize"));
assert.ok(!covered("/settings/connected-apps"));
});

test("self-host serves the OpenAPI document through CloudFront", () => {
assert.ok(API_PATHS.includes("/openapi.json"));
});
Expand Down
4 changes: 2 additions & 2 deletions services/gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ uuid = { version = "1", features = ["v4"] }
regex = "1"
md5 = "0.7"
futures = "0.3"
# mcp_server: a tool call runs through the API router (ServiceExt::oneshot)
tower = { version = "0.5", features = ["util"] }
tokio-stream = "0.1"

# MenteDB (agent memory browsing)
Expand All @@ -86,7 +88,5 @@ tar = "0.4"
[dev-dependencies]
# rsa 0.9 key generation needs the rand 0.8 RNG traits (tests only)
rand08 = { package = "rand", version = "0.8" }
# middleware tests drive a small router
tower = { version = "0.5", features = ["util"] }
# signs test messages the way CoderHelm's KMS key does (hosted_sig)
ed25519-dalek = { version = "2", default-features = false, features = ["std"] }
54 changes: 54 additions & 0 deletions services/gateway/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilte
mod api_keys;
mod auth;
mod clients;
mod mcp_oauth;
mod mcp_server;
mod middleware;
mod model_call;
mod models;
Expand Down Expand Up @@ -395,6 +397,9 @@ async fn main() -> Result<(), Error> {
)
.route("/search", get(routes::search::search))
.route("/runs", get(routes::api::list_runs))
// A person starts a run on a ticket from outside its tracker (a
// connected app's start_run).
.route("/runs/start", post(routes::run_start::start_run))
.route("/runs/:run_id", get(routes::api::get_run))
.route("/runs/:run_id/traces", get(routes::api::get_run_traces))
.route("/runs/:run_id/agent-log", get(routes::api::get_agent_log))
Expand Down Expand Up @@ -637,6 +642,18 @@ async fn main() -> Result<(), Error> {
get(routes::api_keys::list).post(routes::api_keys::create),
)
.route("/api-keys/:key_id", delete(routes::api_keys::revoke))
// Connecting apps to CoderHelm's MCP server: the consent page, and
// the apps people connected (routes::mcp_oauth).
.route(
"/mcp/authorize/describe",
post(routes::mcp_oauth::describe_request),
)
.route("/mcp/authorize", post(routes::mcp_oauth::decide_request))
.route("/mcp/connections", get(routes::mcp_oauth::list_connections))
.route(
"/mcp/connections/:grant_id",
delete(routes::mcp_oauth::revoke_connection),
)
.route(
"/roles/:role_id",
put(routes::roles::update_role).delete(routes::roles::delete_role),
Expand Down Expand Up @@ -1396,6 +1413,21 @@ async fn main() -> Result<(), Error> {
middleware::rate_limit::rate_limit_email,
));

// Signing in to CoderHelm's MCP server (public; routes::mcp_oauth).
let oauth_routes = Router::new()
.route("/register", post(routes::mcp_oauth::register))
.route("/authorize", get(routes::mcp_oauth::authorize))
.route("/token", post(routes::mcp_oauth::token))
.route("/revoke", post(routes::mcp_oauth::revoke));

// The tools of the MCP server call the API router as the person who
// connected the app, through its auth middleware (mcp_server::call).
let mcp_api = mcp_server::McpApi(
Router::new()
.nest("/api", api_routes.clone())
.with_state(state.clone()),
);

let app = Router::new()
// Root health/info
.route(
Expand Down Expand Up @@ -1475,6 +1507,28 @@ async fn main() -> Result<(), Error> {
routes::mcp_servers::METADATA_DOCUMENT_PATH,
get(routes::mcp_servers::client_metadata_document),
)
// CoderHelm's MCP server, for Claude, Cursor, VS Code and other MCP
// clients (mcp_server), and where its clients learn how to sign in.
.route(
"/mcp",
post(mcp_server::post)
.get(mcp_server::not_allowed)
.delete(mcp_server::not_allowed)
.layer(axum::Extension(mcp_api)),
)
.route(
"/.well-known/oauth-protected-resource",
get(routes::mcp_oauth::protected_resource),
)
.route(
"/.well-known/oauth-protected-resource/mcp",
get(routes::mcp_oauth::protected_resource),
)
.route(
"/.well-known/oauth-authorization-server",
get(routes::mcp_oauth::authorization_server),
)
.nest("/oauth", oauth_routes)
// The sign-in methods that work here: the sign-in page reads it on
// every load, so it stays out of the sign-in attempts' rate limit.
.route("/auth/methods", get(routes::auth::methods))
Expand Down
Loading
Loading