v2: Web standards Request object in ctx (#1822)
Co-authored-by: Felix Weinberger <3823880+felixweinberger@users.noreply.github.com>
This commit is contained in:
parent
f73a5af4bc
commit
2fd7f5ff26
2 changed files with 81 additions and 42 deletions
|
|
@ -155,8 +155,7 @@ export class Server extends Protocol<ServerContext> {
|
|||
|
||||
protected override buildContext(ctx: BaseContext, transportInfo?: MessageExtraInfo): ServerContext {
|
||||
// Only create http when there's actual HTTP transport info or auth info
|
||||
const hasHttpInfo =
|
||||
ctx.http || transportInfo?.requestInfo || transportInfo?.closeSSEStream || transportInfo?.closeStandaloneSSEStream;
|
||||
const hasHttpInfo = ctx.http || transportInfo?.request || transportInfo?.closeSSEStream || transportInfo?.closeStandaloneSSEStream;
|
||||
return {
|
||||
...ctx,
|
||||
mcpReq: {
|
||||
|
|
@ -168,7 +167,7 @@ export class Server extends Protocol<ServerContext> {
|
|||
http: hasHttpInfo
|
||||
? {
|
||||
...ctx.http,
|
||||
req: transportInfo?.requestInfo,
|
||||
req: transportInfo?.request,
|
||||
closeSSE: transportInfo?.closeSSEStream,
|
||||
closeStandaloneSSE: transportInfo?.closeStandaloneSSEStream
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
* For Node.js Express/HTTP compatibility, use {@linkcode @modelcontextprotocol/node!NodeStreamableHTTPServerTransport | NodeStreamableHTTPServerTransport} which wraps this transport.
|
||||
*/
|
||||
|
||||
import type { AuthInfo, JSONRPCMessage, MessageExtraInfo, RequestId, RequestInfo, Transport } from '@modelcontextprotocol/core';
|
||||
import type { AuthInfo, JSONRPCMessage, MessageExtraInfo, RequestId, Transport } from '@modelcontextprotocol/core';
|
||||
import {
|
||||
DEFAULT_NEGOTIATED_PROTOCOL_VERSION,
|
||||
isInitializeRequest,
|
||||
|
|
@ -634,10 +634,7 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
|
|||
return this.createJsonErrorResponse(415, -32_000, 'Unsupported Media Type: Content-Type must be application/json');
|
||||
}
|
||||
|
||||
// Build request info from headers
|
||||
const requestInfo: RequestInfo = {
|
||||
headers: req.headers
|
||||
};
|
||||
const request = req;
|
||||
|
||||
let rawMessage;
|
||||
if (options?.parsedBody === undefined) {
|
||||
|
|
@ -707,7 +704,7 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
|
|||
if (!hasRequests) {
|
||||
// if it only contains notifications or responses, return 202
|
||||
for (const message of messages) {
|
||||
this.onmessage?.(message, { authInfo: options?.authInfo, requestInfo });
|
||||
this.onmessage?.(message, { authInfo: options?.authInfo, request });
|
||||
}
|
||||
return new Response(null, { status: 202 });
|
||||
}
|
||||
|
|
@ -741,7 +738,7 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
|
|||
}
|
||||
|
||||
for (const message of messages) {
|
||||
this.onmessage?.(message, { authInfo: options?.authInfo, requestInfo });
|
||||
this.onmessage?.(message, { authInfo: options?.authInfo, request });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -811,7 +808,7 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
|
|||
};
|
||||
}
|
||||
|
||||
this.onmessage?.(message, { authInfo: options?.authInfo, requestInfo, closeSSEStream, closeStandaloneSSEStream });
|
||||
this.onmessage?.(message, { authInfo: options?.authInfo, request, closeSSEStream, closeStandaloneSSEStream });
|
||||
}
|
||||
// The server SHOULD NOT close the SSE stream before sending all JSON-RPC responses
|
||||
// This will be handled by the send() method when responses are ready
|
||||
|
|
|
|||
Loading…
Reference in a new issue