Skip to content

Commit e757029

Browse files
feat(mcp): expose client options in streamableHTTPApp
1 parent fe4dddb commit e757029

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

packages/mcp-server/src/http.ts

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@ import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/
66
import express from 'express';
77
import { fromError } from 'zod-validation-error/v3';
88
import { McpOptions, parseQueryOptions } from './options';
9-
import { initMcpServer, newMcpServer } from './server';
9+
import { ClientOptions, initMcpServer, newMcpServer } from './server';
1010
import { parseAuthHeaders } from './headers';
1111

12-
const newServer = (
13-
defaultMcpOptions: McpOptions,
14-
req: express.Request,
15-
res: express.Response,
16-
): McpServer | null => {
12+
const newServer = ({
13+
clientOptions,
14+
mcpOptions: defaultMcpOptions,
15+
req,
16+
res,
17+
}: {
18+
clientOptions: ClientOptions;
19+
mcpOptions: McpOptions;
20+
req: express.Request;
21+
res: express.Response;
22+
}): McpServer | null => {
1723
const server = newMcpServer();
1824

1925
let mcpOptions: McpOptions;
@@ -35,10 +41,8 @@ const newServer = (
3541
initMcpServer({
3642
server: server,
3743
clientOptions: {
44+
...clientOptions,
3845
...authOptions,
39-
defaultHeaders: {
40-
'X-Stainless-MCP': 'true',
41-
},
4246
},
4347
mcpOptions,
4448
});
@@ -56,17 +60,19 @@ const newServer = (
5660
return server;
5761
};
5862

59-
const post = (defaultOptions: McpOptions) => async (req: express.Request, res: express.Response) => {
60-
const server = newServer(defaultOptions, req, res);
61-
// If we return null, we already set the authorization error.
62-
if (server === null) return;
63-
const transport = new StreamableHTTPServerTransport({
64-
// Stateless server
65-
sessionIdGenerator: undefined,
66-
});
67-
await server.connect(transport);
68-
await transport.handleRequest(req, res, req.body);
69-
};
63+
const post =
64+
(options: { clientOptions: ClientOptions; mcpOptions: McpOptions }) =>
65+
async (req: express.Request, res: express.Response) => {
66+
const server = newServer({ ...options, req, res });
67+
// If we return null, we already set the authorization error.
68+
if (server === null) return;
69+
const transport = new StreamableHTTPServerTransport({
70+
// Stateless server
71+
sessionIdGenerator: undefined,
72+
});
73+
await server.connect(transport);
74+
await transport.handleRequest(req, res, req.body);
75+
};
7076

7177
const get = async (req: express.Request, res: express.Response) => {
7278
res.status(405).json({
@@ -88,20 +94,26 @@ const del = async (req: express.Request, res: express.Response) => {
8894
});
8995
};
9096

91-
export const streamableHTTPApp = (options: McpOptions): express.Express => {
97+
export const streamableHTTPApp = ({
98+
clientOptions = {},
99+
mcpOptions = {},
100+
}: {
101+
clientOptions?: ClientOptions;
102+
mcpOptions?: McpOptions;
103+
}): express.Express => {
92104
const app = express();
93105
app.set('query parser', 'extended');
94106
app.use(express.json());
95107

96108
app.get('/', get);
97-
app.post('/', post(options));
109+
app.post('/', post({ clientOptions, mcpOptions }));
98110
app.delete('/', del);
99111

100112
return app;
101113
};
102114

103115
export const launchStreamableHTTPServer = async (options: McpOptions, port: number | string | undefined) => {
104-
const app = streamableHTTPApp(options);
116+
const app = streamableHTTPApp({ mcpOptions: options });
105117
const server = app.listen(port);
106118
const address = server.address();
107119

0 commit comments

Comments
 (0)