Skip to content

Incoming request headers in passed to callbacks via RequestHandlerExtra<ServerRequest, ServerNotification> #557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/server/sse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { randomUUID } from "node:crypto";
import { IncomingMessage, ServerResponse } from "node:http";
import { IncomingHttpHeaders, IncomingMessage, ServerResponse } from "node:http";
import { Transport } from "../shared/transport.js";
import { JSONRPCMessage, JSONRPCMessageSchema } from "../types.js";
import getRawBody from "raw-body";
Expand All @@ -20,7 +20,7 @@ export class SSEServerTransport implements Transport {

onclose?: () => void;
onerror?: (error: Error) => void;
onmessage?: (message: JSONRPCMessage, extra?: { authInfo?: AuthInfo }) => void;
onmessage?: (message: JSONRPCMessage, extra?: { authInfo?: AuthInfo, requestHeaders?: IncomingHttpHeaders }) => void;

/**
* Creates a new SSE server transport, which will direct the client to POST messages to the relative or absolute URL identified by `_endpoint`.
Expand Down Expand Up @@ -106,7 +106,7 @@ export class SSEServerTransport implements Transport {
}

try {
await this.handleMessage(typeof body === 'string' ? JSON.parse(body) : body, { authInfo });
await this.handleMessage(typeof body === 'string' ? JSON.parse(body) : body, { authInfo, requestHeaders: req.headers });
} catch {
res.writeHead(400).end(`Invalid message: ${body}`);
return;
Expand All @@ -118,7 +118,7 @@ export class SSEServerTransport implements Transport {
/**
* Handle a client message, regardless of how it arrived. This can be used to inform the server of messages that arrive via a means different than HTTP POST.
*/
async handleMessage(message: unknown, extra?: { authInfo?: AuthInfo }): Promise<void> {
async handleMessage(message: unknown, extra?: { authInfo?: AuthInfo, requestHeaders: IncomingHttpHeaders }): Promise<void> {
let parsedMessage: JSONRPCMessage;
try {
parsedMessage = JSONRPCMessageSchema.parse(message);
Expand Down
8 changes: 4 additions & 4 deletions src/server/streamableHttp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IncomingMessage, ServerResponse } from "node:http";
import { IncomingHttpHeaders, IncomingMessage, ServerResponse } from "node:http";
import { Transport } from "../shared/transport.js";
import { isInitializeRequest, isJSONRPCError, isJSONRPCRequest, isJSONRPCResponse, JSONRPCMessage, JSONRPCMessageSchema, RequestId } from "../types.js";
import getRawBody from "raw-body";
Expand Down Expand Up @@ -113,7 +113,7 @@ export class StreamableHTTPServerTransport implements Transport {
sessionId?: string | undefined;
onclose?: () => void;
onerror?: (error: Error) => void;
onmessage?: (message: JSONRPCMessage, extra?: { authInfo?: AuthInfo }) => void;
onmessage?: (message: JSONRPCMessage, extra?: { authInfo?: AuthInfo, requestHeaders?: IncomingHttpHeaders }) => void;

constructor(options: StreamableHTTPServerTransportOptions) {
this.sessionIdGenerator = options.sessionIdGenerator;
Expand Down Expand Up @@ -395,7 +395,7 @@ export class StreamableHTTPServerTransport implements Transport {

// handle each message
for (const message of messages) {
this.onmessage?.(message, { authInfo });
this.onmessage?.(message, { authInfo, requestHeaders: req.headers });
}
} else if (hasRequests) {
// The default behavior is to use SSE streaming
Expand Down Expand Up @@ -430,7 +430,7 @@ export class StreamableHTTPServerTransport implements Transport {

// handle each message
for (const message of messages) {
this.onmessage?.(message, { authInfo });
this.onmessage?.(message, { authInfo, requestHeaders: req.headers });
}
// 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
Expand Down
9 changes: 8 additions & 1 deletion src/shared/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from "../types.js";
import { Transport, TransportSendOptions } from "./transport.js";
import { AuthInfo } from "../server/auth/types.js";
import { IncomingHttpHeaders } from "node:http";

/**
* Callback for progress notifications.
Expand Down Expand Up @@ -127,6 +128,11 @@ export type RequestHandlerExtra<SendRequestT extends Request,
*/
requestId: RequestId;

/**
* The headers from the original request.
*/
requestHeaders: IncomingHttpHeaders;

/**
* Sends a notification that relates to the current request being handled.
*
Expand Down Expand Up @@ -339,7 +345,7 @@ export abstract class Protocol<
);
}

private _onrequest(request: JSONRPCRequest, extra?: { authInfo?: AuthInfo }): void {
private _onrequest(request: JSONRPCRequest, extra?: { authInfo?: AuthInfo, requestHeaders?: IncomingHttpHeaders }): void {
const handler =
this._requestHandlers.get(request.method) ?? this.fallbackRequestHandler;

Expand Down Expand Up @@ -375,6 +381,7 @@ export abstract class Protocol<
this.request(r, resultSchema, { ...options, relatedRequestId: request.id }),
authInfo: extra?.authInfo,
requestId: request.id,
requestHeaders: extra?.requestHeaders ?? {},
};

// Starting with Promise.resolve() puts any synchronous errors into the monad as well.
Expand Down
3 changes: 2 additions & 1 deletion src/shared/transport.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IncomingHttpHeaders } from "http";
import { AuthInfo } from "../server/auth/types.js";
import { JSONRPCMessage, RequestId } from "../types.js";

Expand Down Expand Up @@ -69,7 +70,7 @@ export interface Transport {
* Includes the authInfo if the transport is authenticated.
*
*/
onmessage?: (message: JSONRPCMessage, extra?: { authInfo?: AuthInfo }) => void;
onmessage?: (message: JSONRPCMessage, extra?: { authInfo?: AuthInfo, requestHeaders?: IncomingHttpHeaders }) => void;

/**
* The session ID generated for this connection.
Expand Down