feat(mcp): mount SDK auth router on Express#1142
Merged
Merged
Conversation
Owner
Author
This was referenced May 23, 2026
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2d7a41f9ab
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
This was referenced May 24, 2026
2d7a41f to
4ad56e1
Compare
Owner
Author
Merge activity
|
4ad56e1 to
26db882
Compare
Comment on lines
+232
to
+246
| app.use((req, res, next) => { | ||
| const boundPort = getBoundPort(server); | ||
| const pathname = normalizeRoutePath(req.path); | ||
| if (pathname === "/mcp") { | ||
| if (isAllowedMcpHttpRequest(req, boundPort, configuredPublicMcpUrl)) return next(); | ||
| res.status(403).type("text/plain").send("Forbidden"); | ||
| return; | ||
| } | ||
| if (isOAuthOrMetadataPath(pathname)) { | ||
| if (isAllowedOAuthHttpRequest(req, boundPort, configuredPublicMcpUrl)) return next(); | ||
| res.status(403).type("text/plain").send("Forbidden"); | ||
| return; | ||
| } | ||
| next(); | ||
| }); |
| next(); | ||
| }); | ||
|
|
||
| app.use(auditOAuthRouteResponses(auditEmit)); |
Comment on lines
+250
to
+321
| app.get("/oauth/callback", async (req, res) => { | ||
| const remoteAddress = req.socket.remoteAddress ?? undefined; | ||
| if (!oidcConfig) { | ||
| auditEmit( | ||
| buildOAuthAuditEvent("oidc_callback", { | ||
| outcome: "denied", | ||
| reason: "oidc_not_configured", | ||
| remoteAddress, | ||
| }), | ||
| ); | ||
| res.status(400).json({ | ||
| error: "temporarily_unavailable", | ||
| error_description: "OIDC is not configured", | ||
| }); | ||
| return; | ||
| } | ||
| const completed = await completeOidcAuthorization( | ||
| new URLSearchParams(req.query as Record<string, string>), | ||
| oidcPendingStore, | ||
| oidcConfig, | ||
| publicMcpUrl, | ||
| ); | ||
| if ("status" in completed) { | ||
| auditEmit( | ||
| buildOAuthAuditEvent("oidc_callback", { | ||
| outcome: "denied", | ||
| reason: completed.body.error, | ||
| remoteAddress, | ||
| }), | ||
| ); | ||
| res.status(completed.status).json(completed.body); | ||
| return; | ||
| } | ||
| const oauthClientId = completed.oauthParams.get("client_id") ?? undefined; | ||
| const code = codeStore.issueCode({ | ||
| clientId: completed.oauthParams.get("client_id") ?? "", | ||
| redirectUri: completed.oauthParams.get("redirect_uri") ?? "", | ||
| codeChallenge: completed.oauthParams.get("code_challenge") ?? "", | ||
| ...(completed.oauthParams.get("resource") | ||
| ? { resource: completed.oauthParams.get("resource") ?? undefined } | ||
| : {}), | ||
| expiresAt: Date.now() + 5 * 60 * 1000, | ||
| }); | ||
| if (!code) { | ||
| auditEmit( | ||
| buildOAuthAuditEvent("oidc_callback", { | ||
| outcome: "denied", | ||
| reason: "temporarily_unavailable", | ||
| clientId: oauthClientId, | ||
| remoteAddress, | ||
| }), | ||
| ); | ||
| res.status(400).json({ | ||
| error: "temporarily_unavailable", | ||
| error_description: "Too many active authorization codes", | ||
| }); | ||
| return; | ||
| } | ||
| const redirect = new URL(completed.oauthParams.get("redirect_uri") ?? ""); | ||
| redirect.searchParams.set("code", code); | ||
| const state = completed.oauthParams.get("state"); | ||
| if (state) redirect.searchParams.set("state", state); | ||
| auditEmit( | ||
| buildOAuthAuditEvent("oidc_callback", { | ||
| outcome: "success", | ||
| reason: "code_issued", | ||
| clientId: oauthClientId, | ||
| remoteAddress, | ||
| }), | ||
| ); | ||
| res.redirect(302, redirect.href); | ||
| }); |
| const bearerAuditMiddleware = shouldRequireMcpBearer | ||
| ? auditBearerPreflight(auditEmit, tokenStore) | ||
| : (_req: Request, _res: Response, next: NextFunction) => next(); | ||
| app.post("/mcp", bearerAuditMiddleware, bearerMiddleware, async (req, res) => { |
Comment on lines
+343
to
+364
| app.post("/mcp", bearerAuditMiddleware, bearerMiddleware, async (req, res) => { | ||
| if (req.auth) { | ||
| auditEmit( | ||
| buildOAuthAuditEvent("bearer", { | ||
| outcome: "success", | ||
| clientId: req.auth.clientId, | ||
| remoteAddress: req.socket.remoteAddress ?? undefined, | ||
| }), | ||
| ); | ||
| } | ||
| const mcpServer = createCodememMcpServer(store); | ||
| const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: undefined }); | ||
| const activeRequest = { mcpServer }; | ||
| activeRequests.add(activeRequest); | ||
| try { | ||
| await mcpServer.connect(transport); | ||
| await transport.handleRequest(req, res); | ||
| } finally { | ||
| activeRequests.delete(activeRequest); | ||
| await mcpServer.close(); | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Description
Migrates the MCP HTTP server from hand-rolled
node:httpOAuth routing to Express mounted with the MCP SDKmcpAuthRouterandrequireBearerAuth.Key points:
/oauth/callbackcodemem-owned so upstream OIDC can complete and issue the MCP authorization-code redirect./register,/authorize,/token,/revoke, and OAuth metadata./mcprequests through SDK bearer auth.Type of Change
Testing
pnpm run tsc,pnpm run lint,pnpm run test)Validation run:
pnpm run lintpnpm run tscpnpm exec vitest run packages/mcp-server/src/http.test.ts packages/mcp-server/src/oauth.test.ts packages/mcp-server/src/provider.test.tspnpm run testChecklist
pnpm run lintpasses for touched files)Docs are deferred to the later remote MCP OAuth docs/release slice after the remaining refresh-token/resource-binding work lands.