From 36aa2ca92e8a82f1eb48b18351334c9f462a76a8 Mon Sep 17 00:00:00 2001 From: Christopher Pappas Date: Sat, 22 Nov 2025 23:05:35 +0100 Subject: [PATCH] fix: dont attempt to reconnect to failed servers --- .gitignore | 1 + agent-chat-cli.config.ts | 2 +- src/prompts/chrome.md | 1 + src/prompts/github.md | 2 +- src/prompts/system.md | 1 - src/utils/mcpServerSelectionAgent.ts | 33 ++++++++++++++++++++++++++-- src/utils/runAgentLoop.ts | 1 + 7 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 src/prompts/chrome.md diff --git a/.gitignore b/.gitignore index 5cd0b9a..63c8a5d 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,4 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json .DS_Store .claude/settings.local.json +.npmrc diff --git a/agent-chat-cli.config.ts b/agent-chat-cli.config.ts index 9546510..823a6c7 100644 --- a/agent-chat-cli.config.ts +++ b/agent-chat-cli.config.ts @@ -25,7 +25,7 @@ const config: AgentChatConfig = { }, github: { description: - "GitHub MCP tools to search code, PRs, issues; discover documentation in repo docs/; find deployment guides and code examples.", + "GitHub MCP tools to search remote code, PRs, issues; discover documentation in remote repo docs/; find deployment guides and code examples.", prompt: getPrompt("github.md"), command: "bunx", args: [ diff --git a/src/prompts/chrome.md b/src/prompts/chrome.md new file mode 100644 index 0000000..e261dd0 --- /dev/null +++ b/src/prompts/chrome.md @@ -0,0 +1 @@ +# System Prompt for Chrome Devtools MCP diff --git a/src/prompts/github.md b/src/prompts/github.md index af0f193..a6b29b6 100644 --- a/src/prompts/github.md +++ b/src/prompts/github.md @@ -1,6 +1,6 @@ # System Prompt for Github MCP Server Agent -You are a GitHub MCP server agent, optimized for performing operations on github repos. +You are a GitHub MCP server agent, optimized for performing operations on remote github repos. ### Core Capabilities diff --git a/src/prompts/system.md b/src/prompts/system.md index 65e66fc..752af15 100644 --- a/src/prompts/system.md +++ b/src/prompts/system.md @@ -7,7 +7,6 @@ You are a helpful Agent specifically designed to handle questions related to sys - **CRITICAL**: Only tools prefixed with `mcp_` are to be invoked. Any other tool such as "Bash", etc are strictly forbidden. - **CRITICAL**: When a user attempts to user a tool or MCP server, understand that the first pass is an inference call. If the inference call fails, immediately review the system prompt to see if the tool or MCP server is allowed (ie, CONNECTED). If it is not connected, do not attempt to invoke the tool or MCP server. - **CRITICAL**: When a user starts a convo and asks a question or assigns you a task (example: "in github, please summarize the last merged pr"), before beginning your task (ie, calling tools, etc) respond back immediately with a small summary about what you're going to do, in a friendly kind of way. Then start working. - - **CRITICAL**: If a user starts a convo with a general greeting (like "Hi!" or "Hello!") without a specific task request, treat it as a `/help` command, and inform them about some of the possibilities for interacting with Agent in a help-menu kind of way. Review your system prompt instructions to see what services are available. **DO NOT INVOKE ANY TOOLS AT THIS STEP, JUST OUTPUT A SUMMARY** diff --git a/src/utils/mcpServerSelectionAgent.ts b/src/utils/mcpServerSelectionAgent.ts index 4056a51..805041b 100644 --- a/src/utils/mcpServerSelectionAgent.ts +++ b/src/utils/mcpServerSelectionAgent.ts @@ -5,6 +5,7 @@ import { type SDKUserMessage, } from "@anthropic-ai/claude-agent-sdk" import type { AgentConfig } from "utils/createAgent" +import type { McpServerStatus } from "store" import { log } from "utils/logger" import { z } from "zod" import { messageTypes } from "./runAgentLoop" @@ -14,6 +15,7 @@ interface SelectMcpServersOptions { agents?: Record inferredServers?: Set enabledMcpServers: Record | undefined + mcpServers?: McpServerStatus[] onServerConnection?: (status: string) => void sessionId?: string userMessage: string @@ -24,6 +26,7 @@ export const inferMcpServers = async ({ agents, inferredServers = new Set(), enabledMcpServers, + mcpServers: mcpServerStatuses = [], onServerConnection, sessionId, userMessage, @@ -158,7 +161,33 @@ Examples: log("[mcpServerSelectionAgent] Selected MCP servers:", selectedServers) - const newServers = selectedServers.filter( + const isRetryRequest = /retry|reconnect/i.test(userMessage) + + const failedServers = new Set( + mcpServerStatuses + .filter((s) => s.status === "failed") + .map((s) => s.name.toLowerCase()) + ) + + const serversAfterFailureFilter = isRetryRequest + ? selectedServers + : selectedServers.filter( + (server) => !failedServers.has(server.toLowerCase()) + ) + + if (!isRetryRequest && failedServers.size > 0) { + const filteredOut = selectedServers.filter((server) => + failedServers.has(server.toLowerCase()) + ) + if (filteredOut.length > 0) { + log( + "[mcpServerSelectionAgent] Excluding failed servers (not a retry):", + filteredOut.join(", ") + ) + } + } + + const newServers = serversAfterFailureFilter.filter( (server) => !inferredServers.has(server.toLowerCase()) ) @@ -173,7 +202,7 @@ Examples: const allServers = new Set([ ...Array.from(inferredServers), - ...selectedServers, + ...serversAfterFailureFilter, ]) const mcpServers = diff --git a/src/utils/runAgentLoop.ts b/src/utils/runAgentLoop.ts index 9fd4c5f..0aad794 100644 --- a/src/utils/runAgentLoop.ts +++ b/src/utils/runAgentLoop.ts @@ -65,6 +65,7 @@ export async function* runAgentLoop({ agents: config.agents, inferredServers, enabledMcpServers, + mcpServers, onServerConnection, sessionId, userMessage,