-
Notifications
You must be signed in to change notification settings - Fork 138
feat: add client idle timeout [MCP-57] #383
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,132 @@ | ||
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js"; | ||
import logger, { LogId } from "./logger.js"; | ||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; | ||
import logger, { LogId, McpLogger } from "./logger.js"; | ||
|
||
class TimeoutManager { | ||
|
||
private timeoutId?: NodeJS.Timeout; | ||
public onerror?: (error: unknown) => void; | ||
|
||
constructor( | ||
private readonly callback: () => Promise<void> | void, | ||
private readonly timeoutMS: number | ||
) { | ||
if (timeoutMS <= 0) { | ||
throw new Error("timeoutMS must be greater than 0"); | ||
} | ||
this.reset(); | ||
} | ||
|
||
clear() { | ||
if (this.timeoutId) { | ||
clearTimeout(this.timeoutId); | ||
this.timeoutId = undefined; | ||
} | ||
} | ||
|
||
private async runCallback() { | ||
if (this.callback) { | ||
try { | ||
await this.callback(); | ||
} catch (error: unknown) { | ||
this.onerror?.(error); | ||
} | ||
} | ||
} | ||
|
||
reset() { | ||
this.clear(); | ||
this.timeoutId = setTimeout(() => { | ||
void this.runCallback().finally(() => { | ||
this.timeoutId = undefined; | ||
}); | ||
}, this.timeoutMS); | ||
} | ||
} | ||
|
||
export class SessionStore { | ||
private sessions: { [sessionId: string]: StreamableHTTPServerTransport } = {}; | ||
private sessions: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [q] didn't check our telemetry PR but should we consider adding some http metrics? we could open a ticket for this, just checking about what you think. example: number of active sessions, timed out sessions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does that add value? number of active sessions changes over time not sure how we would track it, we can add a time out session but bear in mind all of our telemetry is session based not server based, as it it lives inside of the MCP server not outside (http server). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fair point - we can always check w/ analytics first hence the idea of a ticket |
||
[sessionId: string]: { | ||
mcpServer: McpServer; | ||
transport: StreamableHTTPServerTransport; | ||
abortTimeout: TimeoutManager; | ||
notificationTimeout: TimeoutManager; | ||
}; | ||
} = {}; | ||
|
||
constructor( | ||
private readonly idleTimeoutMS: number, | ||
private readonly notificationTimeoutMS: number | ||
) { | ||
if (idleTimeoutMS <= 0) { | ||
throw new Error("idleTimeoutMS must be greater than 0"); | ||
} | ||
if (notificationTimeoutMS <= 0) { | ||
throw new Error("notificationTimeoutMS must be greater than 0"); | ||
} | ||
if (idleTimeoutMS <= notificationTimeoutMS) { | ||
throw new Error("idleTimeoutMS must be greater than notificationTimeoutMS"); | ||
} | ||
} | ||
|
||
getSession(sessionId: string): StreamableHTTPServerTransport | undefined { | ||
return this.sessions[sessionId]; | ||
this.resetTimeout(sessionId); | ||
return this.sessions[sessionId]?.transport; | ||
} | ||
|
||
private resetTimeout(sessionId: string): void { | ||
const session = this.sessions[sessionId]; | ||
if (!session) { | ||
return; | ||
} | ||
|
||
session.abortTimeout.reset(); | ||
|
||
session.notificationTimeout.reset(); | ||
} | ||
|
||
setSession(sessionId: string, transport: StreamableHTTPServerTransport): void { | ||
private sendNotification(sessionId: string): void { | ||
const session = this.sessions[sessionId]; | ||
if (!session) { | ||
return; | ||
} | ||
const logger = new McpLogger(session.mcpServer); | ||
logger.info( | ||
LogId.streamableHttpTransportSessionCloseNotification, | ||
"sessionStore", | ||
"Session is about to be closed due to inactivity" | ||
); | ||
} | ||
|
||
setSession(sessionId: string, transport: StreamableHTTPServerTransport, mcpServer: McpServer): void { | ||
if (this.sessions[sessionId]) { | ||
throw new Error(`Session ${sessionId} already exists`); | ||
} | ||
this.sessions[sessionId] = transport; | ||
const abortTimeout = new TimeoutManager(async () => { | ||
const logger = new McpLogger(mcpServer); | ||
logger.info( | ||
LogId.streamableHttpTransportSessionCloseNotification, | ||
"sessionStore", | ||
"Session closed due to inactivity" | ||
); | ||
|
||
await this.closeSession(sessionId); | ||
}, this.idleTimeoutMS); | ||
const notificationTimeout = new TimeoutManager( | ||
() => this.sendNotification(sessionId), | ||
this.notificationTimeoutMS | ||
); | ||
this.sessions[sessionId] = { mcpServer, transport, abortTimeout, notificationTimeout }; | ||
} | ||
|
||
async closeSession(sessionId: string, closeTransport: boolean = true): Promise<void> { | ||
if (!this.sessions[sessionId]) { | ||
throw new Error(`Session ${sessionId} not found`); | ||
} | ||
this.sessions[sessionId].abortTimeout.clear(); | ||
this.sessions[sessionId].notificationTimeout.clear(); | ||
if (closeTransport) { | ||
const transport = this.sessions[sessionId]; | ||
if (!transport) { | ||
throw new Error(`Session ${sessionId} not found`); | ||
} | ||
try { | ||
await transport.close(); | ||
await this.sessions[sessionId].transport.close(); | ||
} catch (error) { | ||
logger.error( | ||
LogId.streamableHttpTransportSessionCloseFailure, | ||
|
@@ -38,11 +139,6 @@ export class SessionStore { | |
} | ||
|
||
async closeAllSessions(): Promise<void> { | ||
await Promise.all( | ||
Object.values(this.sessions) | ||
.filter((transport) => transport !== undefined) | ||
.map((transport) => transport.close()) | ||
); | ||
this.sessions = {}; | ||
await Promise.all(Object.keys(this.sessions).map((sessionId) => this.closeSession(sessionId))); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can we add unit tests for this?