Skip to content
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
5 changes: 4 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ async function runServer(port: number | null) {
console.log('Received message');

const sessionId = req.query.sessionId as string;
const transport = servers.map(s => s.transport as SSEServerTransport).find(t => t.sessionId === sessionId);

const transport = servers
.reduce((pre, val) => pre.concat(Array.from(val.transportMap.values()) as SSEServerTransport[]), [] as SSEServerTransport[])
.find(t => t.sessionId === sessionId);
if (!transport) {
res.status(404).send('Session not found');
return;
Expand Down
2 changes: 1 addition & 1 deletion src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class Client<
* The new capabilities will be merged with any existing capabilities previously given (e.g., at initialization).
*/
public registerCapabilities(capabilities: ClientCapabilities): void {
if (this.transport) {
if (this.transportMap.size > 0) {
throw new Error('Cannot register capabilities after connecting to transport');
}

Expand Down
4 changes: 4 additions & 0 deletions src/inMemory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export class InMemoryTransport implements Transport {
onerror?: (error: Error) => void;
onmessage?: (message: JSONRPCMessage, extra?: { authInfo?: AuthInfo }) => void;
sessionId?: string;
onsessionclosed?: ((sessionId: string) => void | Promise<void>) | undefined;
onsessioninitialized?: ((sessionId: string) => void | Promise<void>) | undefined;

/**
* Creates a pair of linked in-memory transports that can communicate with each other. One should be passed to a Client and one to a Server.
Expand All @@ -31,6 +33,7 @@ export class InMemoryTransport implements Transport {
}

async start(): Promise<void> {
this.onsessioninitialized?.(this.sessionId!);
// Process any messages that were queued before start was called
while (this._messageQueue.length > 0) {
const queuedMessage = this._messageQueue.shift()!;
Expand All @@ -39,6 +42,7 @@ export class InMemoryTransport implements Transport {
}

async close(): Promise<void> {
this.onsessionclosed?.(this.sessionId!);
const other = this._otherTransport;
this._otherTransport = undefined;
await other?.close();
Expand Down
8 changes: 4 additions & 4 deletions src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class Server<
* The new capabilities will be merged with any existing capabilities previously given (e.g., at initialization).
*/
public registerCapabilities(capabilities: ServerCapabilities): void {
if (this.transport) {
if (this.transportMap.size > 0) {
throw new Error('Cannot register capabilities after connecting to transport');
}
this._capabilities = mergeCapabilities(this._capabilities, capabilities);
Expand Down Expand Up @@ -277,8 +277,8 @@ export class Server<
return this._capabilities;
}

async ping() {
return this.request({ method: 'ping' }, EmptyResultSchema);
async ping(sessionId?: string) {
return this.request({ method: 'ping' }, EmptyResultSchema, { sessionId });
}

async createMessage(params: CreateMessageRequest['params'], options?: RequestOptions) {
Expand Down Expand Up @@ -327,7 +327,7 @@ export class Server<
async sendLoggingMessage(params: LoggingMessageNotification['params'], sessionId?: string) {
if (this._capabilities.logging) {
if (!this.isMessageIgnored(params.level, sessionId)) {
return this.notification({ method: 'notifications/message', params });
return this.notification({ method: 'notifications/message', params }, { sessionId });
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ export class McpServer {
* @returns True if the server is connected
*/
isConnected() {
return this.server.transport !== undefined;
return this.server.transportMap.size > 0;
}

/**
Expand Down
Loading