Skip to content

Commit f385229

Browse files
committed
feat: add url property to RequestInfo interface
Expose the request URL in RequestInfo, allowing server handlers to access query parameters, path info, and other URL components from incoming requests.
1 parent a0c9b13 commit f385229

File tree

6 files changed

+28
-7
lines changed

6 files changed

+28
-7
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@modelcontextprotocol/sdk': minor
3+
---
4+
5+
Add `url` property to `RequestInfo` interface, exposing the request URL to server handlers

src/server/sse.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ export class SSEServerTransport implements Transport {
149149
}
150150

151151
const authInfo: AuthInfo | undefined = req.auth;
152-
const requestInfo: RequestInfo = { headers: req.headers };
152+
const requestInfo: RequestInfo = {
153+
headers: req.headers,
154+
url: req.url
155+
};
153156

154157
let body: string | unknown;
155158
try {

src/server/webStandardStreamableHttp.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,9 +597,10 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
597597
return this.createJsonErrorResponse(415, -32000, 'Unsupported Media Type: Content-Type must be application/json');
598598
}
599599

600-
// Build request info from headers
600+
// Build request info from headers and URL
601601
const requestInfo: RequestInfo = {
602-
headers: Object.fromEntries(req.headers.entries())
602+
headers: Object.fromEntries(req.headers.entries()),
603+
url: req.url
603604
};
604605

605606
let rawMessage;

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2361,6 +2361,10 @@ export interface RequestInfo {
23612361
* The headers of the request.
23622362
*/
23632363
headers: IsomorphicHeaders;
2364+
/**
2365+
* The URL of the request.
2366+
*/
2367+
url?: string;
23642368
}
23652369

23662370
/**

test/server/sse.test.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@ const createMockResponse = () => {
1919
return res as unknown as Mocked<http.ServerResponse>;
2020
};
2121

22-
const createMockRequest = ({ headers = {}, body }: { headers?: Record<string, string>; body?: string } = {}) => {
22+
const createMockRequest = ({
23+
headers = {},
24+
body,
25+
url = '/messages'
26+
}: { headers?: Record<string, string>; body?: string; url?: string } = {}) => {
2327
const mockReq = {
2428
headers,
2529
body: body ? body : undefined,
30+
url,
2631
auth: {
2732
token: 'test-token'
2833
},
@@ -312,7 +317,8 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
312317
'user-agent': 'node',
313318
'accept-encoding': 'gzip, deflate',
314319
'content-length': '124'
315-
}
320+
},
321+
url: `/?sessionId=${sessionId}`
316322
})
317323
}
318324
]
@@ -417,7 +423,8 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
417423
requestInfo: {
418424
headers: {
419425
'content-type': 'application/json'
420-
}
426+
},
427+
url: '/messages'
421428
}
422429
}
423430
);

test/server/streamableHttp.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,8 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
443443
'user-agent': expect.any(String),
444444
'accept-encoding': expect.any(String),
445445
'content-length': expect.any(String)
446-
}
446+
},
447+
url: baseUrl.toString()
447448
});
448449
});
449450

0 commit comments

Comments
 (0)