Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(mcp): reuse sessionID for consecutive MCP tool calls, fix dynamic args clearing, fix refreshing tools on save#2158
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f364c2e
fix(mcp): reuse sessionID for consecutive MCP tool calls, fix dynamic…
waleedlatif1 4545f29
prevent defaults
waleedlatif1 e267f02
fix subblock text area
waleedlatif1 f74c06f
added placeholders in tool-inp for mcp dynamic args
waleedlatif1 e0c7151
ack PR comments
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
59 changes: 56 additions & 3 deletions
59 ...l/components/editor/components/sub-block/components/mcp-dynamic-args/mcp-dynamic-args.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 4 additions & 1 deletion
5 ...ponents/panel/components/editor/components/sub-block/components/tool-input/tool-input.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -49,10 +49,35 @@ class McpService { | ||
| private cacheMisses = 0 | ||
| private entriesEvicted = 0 | ||
| private sessionCache = new Map<string, string>() | ||
waleedlatif1 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| constructor() { | ||
| this.startPeriodicCleanup() | ||
| } | ||
| /** | ||
| * Get cached session ID for a server | ||
| */ | ||
| private getCachedSessionId(serverId: string): string | undefined { | ||
| return this.sessionCache.get(serverId) | ||
| } | ||
| /** | ||
| * Cache session ID for a server | ||
| */ | ||
| private cacheSessionId(serverId: string, sessionId: string): void { | ||
| this.sessionCache.set(serverId, sessionId) | ||
| logger.debug(`Cached session ID for server ${serverId}`) | ||
| } | ||
| /** | ||
| * Clear cached session ID for a server | ||
| */ | ||
| private clearCachedSessionId(serverId: string): void { | ||
| this.sessionCache.delete(serverId) | ||
| logger.debug(`Cleared cached session ID for server ${serverId}`) | ||
| } | ||
| /** | ||
| * Start periodic cleanup of expired cache entries | ||
| */ | ||
| @@ -306,7 +331,7 @@ class McpService { | ||
| } | ||
| /** | ||
| * Create and connect to an MCP client with security policy | ||
| * Create and connect to an MCP client | ||
| */ | ||
| private async createClient(config: McpServerConfig): Promise<McpClient> { | ||
| const securityPolicy = { | ||
| @@ -316,9 +341,49 @@ class McpService { | ||
| allowedOrigins: config.url ? [new URL(config.url).origin] : undefined, | ||
| } | ||
| const client = new McpClient(config, securityPolicy) | ||
| await client.connect() | ||
| return client | ||
| const cachedSessionId = this.getCachedSessionId(config.id) | ||
| const client = new McpClient(config, securityPolicy, cachedSessionId) | ||
| try { | ||
| await client.connect() | ||
| const newSessionId = client.getSessionId() | ||
| if (newSessionId) { | ||
| this.cacheSessionId(config.id, newSessionId) | ||
| } | ||
| return client | ||
| } catch (error) { | ||
| if (cachedSessionId && this.isSessionError(error)) { | ||
| logger.debug(`Session restoration failed for server ${config.id}, retrying fresh`) | ||
| this.clearCachedSessionId(config.id) | ||
| const freshClient = new McpClient(config, securityPolicy) | ||
| await freshClient.connect() | ||
| const freshSessionId = freshClient.getSessionId() | ||
| if (freshSessionId) { | ||
| this.cacheSessionId(config.id, freshSessionId) | ||
| } | ||
| return freshClient | ||
| } | ||
| throw error | ||
| } | ||
| } | ||
| private isSessionError(error: unknown): boolean { | ||
| if (error instanceof Error) { | ||
| const message = error.message.toLowerCase() | ||
| return ( | ||
| message.includes('no valid session') || | ||
| message.includes('invalid session') || | ||
| message.includes('session expired') | ||
| ) | ||
| } | ||
| return false | ||
waleedlatif1 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| /** | ||
| @@ -332,33 +397,25 @@ class McpService { | ||
| ): Promise<McpToolResult> { | ||
| const requestId = generateRequestId() | ||
| try { | ||
| logger.info( | ||
| `[${requestId}] Executing MCP tool ${toolCall.name} on server ${serverId} for user ${userId}` | ||
| ) | ||
| logger.info( | ||
| `[${requestId}] Executing MCP tool ${toolCall.name} on server ${serverId} for user ${userId}` | ||
| ) | ||
| const config = await this.getServerConfig(serverId, workspaceId) | ||
| if (!config) { | ||
| throw new Error(`Server ${serverId} not found or not accessible`) | ||
| } | ||
| const config = await this.getServerConfig(serverId, workspaceId) | ||
| if (!config) { | ||
| throw new Error(`Server ${serverId} not found or not accessible`) | ||
| } | ||
| const resolvedConfig = await this.resolveConfigEnvVars(config, userId, workspaceId) | ||
| const resolvedConfig = await this.resolveConfigEnvVars(config, userId, workspaceId) | ||
| const client = await this.createClient(resolvedConfig) | ||
| const client = await this.createClient(resolvedConfig) | ||
| try { | ||
| const result = await client.callTool(toolCall) | ||
| logger.info(`[${requestId}] Successfully executed tool ${toolCall.name}`) | ||
| return result | ||
| } finally { | ||
| await client.disconnect() | ||
| } | ||
| } catch (error) { | ||
| logger.error( | ||
| `[${requestId}] Failed to execute tool ${toolCall.name} on server ${serverId}:`, | ||
| error | ||
| ) | ||
| throw error | ||
| try { | ||
| const result = await client.callTool(toolCall) | ||
| logger.info(`[${requestId}] Successfully executed tool ${toolCall.name}`) | ||
| return result | ||
| } finally { | ||
| await client.disconnect() | ||
| } | ||
| } | ||
| @@ -442,28 +499,23 @@ class McpService { | ||
| ): Promise<McpTool[]> { | ||
| const requestId = generateRequestId() | ||
| try { | ||
| logger.info(`[${requestId}] Discovering tools from server ${serverId} for user ${userId}`) | ||
| logger.info(`[${requestId}] Discovering tools from server ${serverId} for user ${userId}`) | ||
| const config = await this.getServerConfig(serverId, workspaceId) | ||
| if (!config) { | ||
| throw new Error(`Server ${serverId} not found or not accessible`) | ||
| } | ||
| const config = await this.getServerConfig(serverId, workspaceId) | ||
| if (!config) { | ||
| throw new Error(`Server ${serverId} not found or not accessible`) | ||
| } | ||
| const resolvedConfig = await this.resolveConfigEnvVars(config, userId, workspaceId) | ||
| const resolvedConfig = await this.resolveConfigEnvVars(config, userId, workspaceId) | ||
| const client = await this.createClient(resolvedConfig) | ||
| const client = await this.createClient(resolvedConfig) | ||
| try { | ||
| const tools = await client.listTools() | ||
| logger.info(`[${requestId}] Discovered ${tools.length} tools from server ${config.name}`) | ||
| return tools | ||
| } finally { | ||
| await client.disconnect() | ||
| } | ||
| } catch (error) { | ||
| logger.error(`[${requestId}] Failed to discover tools from server ${serverId}:`, error) | ||
| throw error | ||
| try { | ||
| const tools = await client.listTools() | ||
| logger.info(`[${requestId}] Discovered ${tools.length} tools from server ${config.name}`) | ||
| return tools | ||
| } finally { | ||
| await client.disconnect() | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.