Skip to content

Commit 5f6c688

Browse files
chore(internal): support oauth authorization code flow for MCP servers
1 parent 4b5fcbf commit 5f6c688

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

packages/mcp-server/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@
3434
"@cloudflare/cabidela": "^0.2.4",
3535
"@modelcontextprotocol/sdk": "^1.25.2",
3636
"@valtown/deno-http-worker": "^0.0.21",
37+
"cookie-parser": "^1.4.6",
3738
"cors": "^2.8.5",
3839
"express": "^5.1.0",
3940
"fuse.js": "^7.1.0",
4041
"jq-web": "https://github.com/stainless-api/jq-web/releases/download/v0.8.8/jq-web.tar.gz",
42+
"morgan": "^1.10.0",
4143
"qs": "^6.14.1",
4244
"typescript": "5.8.3",
4345
"yargs": "^17.7.2",
@@ -50,9 +52,11 @@
5052
},
5153
"devDependencies": {
5254
"@anthropic-ai/mcpb": "^2.1.2",
55+
"@types/cookie-parser": "^1.4.10",
5356
"@types/cors": "^2.8.19",
5457
"@types/express": "^5.0.3",
5558
"@types/jest": "^29.4.0",
59+
"@types/morgan": "^1.9.10",
5660
"@types/qs": "^6.14.0",
5761
"@types/yargs": "^17.0.8",
5862
"@typescript-eslint/eslint-plugin": "8.31.1",

packages/mcp-server/src/headers.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { IncomingMessage } from 'node:http';
44
import { ClientOptions } from '@imagekit/nodejs';
55

6-
export const parseAuthHeaders = (req: IncomingMessage): Partial<ClientOptions> => {
6+
export const parseAuthHeaders = (req: IncomingMessage, required?: boolean): Partial<ClientOptions> => {
77
if (req.headers.authorization) {
88
const scheme = req.headers.authorization.split(' ')[0]!;
99
const value = req.headers.authorization.slice(scheme.length + 1);
@@ -19,6 +19,8 @@ export const parseAuthHeaders = (req: IncomingMessage): Partial<ClientOptions> =
1919
'Unsupported authorization scheme. Expected the "Authorization" header to be a supported scheme (Basic).',
2020
);
2121
}
22+
} else if (required) {
23+
throw new Error('Missing required Authorization header; see WWW-Authenticate header for details.');
2224
}
2325

2426
const privateKey =

packages/mcp-server/src/http.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp';
44
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
5-
65
import express from 'express';
6+
import morgan from 'morgan';
77
import { McpOptions } from './options';
88
import { ClientOptions, initMcpServer, newMcpServer } from './server';
99
import { parseAuthHeaders } from './headers';
@@ -20,7 +20,7 @@ const newServer = ({
2020
const server = newMcpServer();
2121

2222
try {
23-
const authOptions = parseAuthHeaders(req);
23+
const authOptions = parseAuthHeaders(req, false);
2424
initMcpServer({
2525
server: server,
2626
clientOptions: {
@@ -75,14 +75,15 @@ const del = async (req: express.Request, res: express.Response) => {
7575

7676
export const streamableHTTPApp = ({
7777
clientOptions = {},
78-
mcpOptions = {},
78+
mcpOptions,
7979
}: {
8080
clientOptions?: ClientOptions;
81-
mcpOptions?: McpOptions;
81+
mcpOptions: McpOptions;
8282
}): express.Express => {
8383
const app = express();
8484
app.set('query parser', 'extended');
8585
app.use(express.json());
86+
app.use(morgan('combined'));
8687

8788
app.get('/', get);
8889
app.post('/', post({ clientOptions, mcpOptions }));

packages/mcp-server/src/options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export function parseCLIOptions(): CLIOptions {
3535
})
3636
.option('port', {
3737
type: 'number',
38+
default: 3000,
3839
description: 'Port to serve on if using http transport',
3940
})
4041
.option('socket', {

0 commit comments

Comments
 (0)