Skip to content

Commit 31f44e0

Browse files
committed
add isInitializeRequest and isInitializedNotification utility
1 parent 09e5d5b commit 31f44e0

File tree

5 files changed

+13
-29
lines changed

5 files changed

+13
-29
lines changed

src/client/streamableHttp.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Transport } from "../shared/transport.js";
2-
import { isJSONRPCNotification, isJSONRPCRequest, isJSONRPCResponse, JSONRPCMessage, JSONRPCMessageSchema } from "../types.js";
2+
import { isInitializedNotification, isJSONRPCNotification, isJSONRPCRequest, isJSONRPCResponse, JSONRPCMessage, JSONRPCMessageSchema } from "../types.js";
33
import { auth, AuthResult, OAuthClientProvider, UnauthorizedError } from "./auth.js";
44
import { EventSourceParserStream } from "eventsource-parser/stream";
55

@@ -420,7 +420,7 @@ export class StreamableHTTPClientTransport implements Transport {
420420
if (response.status === 202) {
421421
// if the accepted notification is initialized, we start the SSE stream
422422
// if it's supported by the server
423-
if (isJSONRPCNotification(message) && message.method === "notifications/initialized") {
423+
if (isInitializedNotification(message)) {
424424
// Start without a lastEventId since this is a fresh connection
425425
this._startOrAuthSse({ resumptionToken: undefined }).catch(err => this.onerror?.(err));
426426
}

src/examples/server/jsonResponseStreamableHttp.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { randomUUID } from 'node:crypto';
33
import { McpServer } from '../../server/mcp.js';
44
import { StreamableHTTPServerTransport } from '../../server/streamableHttp.js';
55
import { z } from 'zod';
6-
import { CallToolResult } from '../../types.js';
6+
import { CallToolResult, isInitializeRequest } from '../../types.js';
77

88
// Create an MCP server with implementation details
99
const server = new McpServer({
@@ -145,14 +145,6 @@ app.get('/mcp', async (req: Request, res: Response) => {
145145
res.status(405).set('Allow', 'POST').send('Method Not Allowed');
146146
});
147147

148-
// Helper function to detect initialize requests
149-
function isInitializeRequest(body: unknown): boolean {
150-
if (Array.isArray(body)) {
151-
return body.some(msg => typeof msg === 'object' && msg !== null && 'method' in msg && msg.method === 'initialize');
152-
}
153-
return typeof body === 'object' && body !== null && 'method' in body && body.method === 'initialize';
154-
}
155-
156148
// Start the server
157149
const PORT = 3000;
158150
app.listen(PORT, () => {

src/examples/server/simpleStreamableHttp.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { randomUUID } from 'node:crypto';
33
import { McpServer } from '../../server/mcp.js';
44
import { EventStore, StreamableHTTPServerTransport } from '../../server/streamableHttp.js';
55
import { z } from 'zod';
6-
import { CallToolResult, GetPromptResult, JSONRPCMessage, ReadResourceResult } from '../../types.js';
6+
import { CallToolResult, GetPromptResult, isInitializeRequest, JSONRPCMessage, ReadResourceResult } from '../../types.js';
77

88
// Create a simple in-memory EventStore for resumability
99
class InMemoryEventStore implements EventStore {
@@ -36,7 +36,7 @@ class InMemoryEventStore implements EventStore {
3636
* Replays events that occurred after a specific event ID
3737
* Implements EventStore.replayEventsAfter
3838
*/
39-
async replayEventsAfter(lastEventId: string,
39+
async replayEventsAfter(lastEventId: string,
4040
{ send }: { send: (eventId: string, message: JSONRPCMessage) => Promise<void> }
4141
): Promise<string> {
4242
if (!lastEventId || !this.events.has(lastEventId)) {
@@ -312,14 +312,6 @@ app.get('/mcp', async (req: Request, res: Response) => {
312312
await transport.handleRequest(req, res);
313313
});
314314

315-
// Helper function to detect initialize requests
316-
function isInitializeRequest(body: unknown): boolean {
317-
if (Array.isArray(body)) {
318-
return body.some(msg => typeof msg === 'object' && msg !== null && 'method' in msg && msg.method === 'initialize');
319-
}
320-
return typeof body === 'object' && body !== null && 'method' in body && body.method === 'initialize';
321-
}
322-
323315
// Start the server
324316
const PORT = 3000;
325317
app.listen(PORT, () => {

src/examples/server/standaloneSseWithGetStreamableHttp.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import express, { Request, Response } from 'express';
22
import { randomUUID } from 'node:crypto';
33
import { McpServer } from '../../server/mcp.js';
44
import { StreamableHTTPServerTransport } from '../../server/streamableHttp.js';
5-
import { ReadResourceResult } from '../../types.js';
5+
import { isInitializeRequest, ReadResourceResult } from '../../types.js';
66

77
// Create an MCP server with implementation details
88
const server = new McpServer({
@@ -107,13 +107,6 @@ app.get('/mcp', async (req: Request, res: Response) => {
107107
await transport.handleRequest(req, res);
108108
});
109109

110-
// Helper function to detect initialize requests
111-
function isInitializeRequest(body: unknown): boolean {
112-
if (Array.isArray(body)) {
113-
return body.some(msg => typeof msg === 'object' && msg !== null && 'method' in msg && msg.method === 'initialize');
114-
}
115-
return typeof body === 'object' && body !== null && 'method' in body && body.method === 'initialize';
116-
}
117110

118111
// Start the server
119112
const PORT = 3000;

src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ export const InitializeRequestSchema = RequestSchema.extend({
248248
}),
249249
});
250250

251+
export const isInitializeRequest = (value: unknown): value is InitializeRequest =>
252+
InitializeRequestSchema.safeParse(value).success;
253+
254+
251255
/**
252256
* Capabilities that a server may support. Known capabilities are defined here, in this schema, but this is not a closed set: any server can define its own, additional capabilities.
253257
*/
@@ -337,6 +341,9 @@ export const InitializedNotificationSchema = NotificationSchema.extend({
337341
method: z.literal("notifications/initialized"),
338342
});
339343

344+
export const isInitializedNotification = (value: unknown): value is InitializedNotification =>
345+
InitializedNotificationSchema.safeParse(value).success;
346+
340347
/* Ping */
341348
/**
342349
* A ping, issued by either the server or the client, to check that the other party is still alive. The receiver must promptly respond, or else may be disconnected.

0 commit comments

Comments
 (0)