Skip to content

Commit 6719b4d

Browse files
committed
feat: pass headers to tool handlers and bump sdk
1 parent c5705f4 commit 6719b4d

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

package-lock.json

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"dependencies": {
1717
"@apidevtools/swagger-parser": "^11.0.0",
18-
"@modelcontextprotocol/sdk": "^1.10.2",
18+
"@modelcontextprotocol/sdk": "^1.17.0",
1919
"dotenv": "^16.5.0",
2020
"es-toolkit": "^1.37.2",
2121
"express": "^5.1.0"
@@ -42,4 +42,4 @@
4242
},
4343
"author": "Postman, Inc.",
4444
"license": "Apache-2.0"
45-
}
45+
}

src/clients/postman.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { IsomorphicHeaders } from '@modelcontextprotocol/sdk/types';
12
import packageJson from '../../package.json' with { type: 'json' };
23
const BASE_URL = 'https://api.postman.com';
34

@@ -6,9 +7,10 @@ export enum ContentType {
67
JsonPatch = 'application/json-patch+json',
78
}
89

9-
export interface FetchPostmanAPIOptions extends RequestInit {
10+
export interface FetchPostmanAPIOptions extends Omit<RequestInit, 'headers'> {
1011
contentType?: ContentType;
1112
apiKey: string;
13+
headers?: IsomorphicHeaders;
1214
}
1315

1416
export async function fetchPostmanAPI(
@@ -21,15 +23,22 @@ export async function fetchPostmanAPI(
2123
}
2224
const contentType = options.contentType || ContentType.Json;
2325

26+
const userAgentHeader =
27+
options.headers && 'user-agent' in options.headers
28+
? `${options.headers['user-agent']}/${packageJson.name}/${packageJson.version}`
29+
: `${packageJson.name}/${packageJson.version}`;
30+
2431
const headers = {
25-
'Content-Type': contentType,
26-
'X-API-Key': apiKey,
27-
'User-Agent': `${packageJson.name}/${packageJson.version}`,
2832
...options.headers,
33+
'content-type': contentType,
34+
'x-api-key': apiKey,
35+
'user-agent': userAgentHeader,
2936
};
3037

38+
const { headers: _, ...optionsWithoutHeaders } = options;
39+
3140
const response = await fetch(`${BASE_URL}${endpoint}`, {
32-
...options,
41+
...optionsWithoutHeaders,
3342
headers,
3443
});
3544

src/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
77
import {
88
CallToolRequestSchema,
99
ErrorCode,
10+
IsomorphicHeaders,
1011
ListToolsRequestSchema,
1112
McpError,
1213
} from '@modelcontextprotocol/sdk/types.js';
@@ -30,7 +31,7 @@ interface ToolModule {
3031
};
3132
handler: (
3233
params: any,
33-
extra: { apiKey: string }
34+
extra: { apiKey: string; headers?: IsomorphicHeaders }
3435
) => Promise<{
3536
content: Array<{ type: string; text: string } & Record<string, unknown>>;
3637
}>;
@@ -125,7 +126,7 @@ async function run() {
125126

126127
logger.info(`Registering ${allGeneratedTools.length} tools...`);
127128

128-
server.setRequestHandler(CallToolRequestSchema, async (request) => {
129+
server.setRequestHandler(CallToolRequestSchema, async (request, extra) => {
129130
const toolName = request.params.name;
130131
const tool = allGeneratedTools.find((t) => t.method === toolName);
131132

@@ -140,7 +141,10 @@ async function run() {
140141
throw new McpError(ErrorCode.InvalidParams, 'API key is required.');
141142
}
142143

143-
const result = await tool.handler(args as any, { apiKey: currentApiKey });
144+
const result = await tool.handler(args as any, {
145+
apiKey: currentApiKey,
146+
headers: extra.requestInfo?.headers,
147+
});
144148
return result;
145149
} catch (error: any) {
146150
throw new McpError(ErrorCode.InternalError, `API error: ${error.message}`, {

0 commit comments

Comments
 (0)