|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "regexp" |
| 8 | + "sort" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/spf13/cobra" |
| 13 | + |
| 14 | + "github.com/smart-mcp-proxy/mcpproxy-go/internal/config" |
| 15 | +) |
| 16 | + |
| 17 | +// The CLAUDE.md / AGENTS.md block documented in |
| 18 | +// docs/features/agent-instructions.md — keep the two in sync. The workflow |
| 19 | +// lines follow the configured routing_mode, since each mode exposes a |
| 20 | +// different set of built-in tools. |
| 21 | +const agentInstructionsHeader = `## MCP tools via mcpproxy |
| 22 | +
|
| 23 | +External services (GitHub, Jira, Slack, databases, cloud APIs, …) are available |
| 24 | +as MCP tools through the ` + "`mcpproxy`" + ` MCP server. |
| 25 | +` |
| 26 | + |
| 27 | +const agentInstructionsRetrieve = `Its tools are NOT listed individually. |
| 28 | +
|
| 29 | +- Before using a shell CLI (` + "`gh`, `aws`, `kubectl`, `curl`" + `, …) or a raw HTTP API |
| 30 | + for an external service, call ` + "`mcpproxy`" + ` ` + "`retrieve_tools`" + ` with the task and |
| 31 | + the service name (e.g. "create github issue"). |
| 32 | +- Call the tool it returns via the ` + "`call_tool_*`" + ` variant named in ` + "`call_with`" + `. |
| 33 | +- If nothing relevant comes back, retry with different wording or just the |
| 34 | + service name before concluding the tool does not exist. |
| 35 | +- Use ` + "`upstream_servers`" + ` (operation ` + "`list`" + `) to see which servers are connected. |
| 36 | +` |
| 37 | + |
| 38 | +const agentInstructionsCodeExecution = `Its tools are NOT listed individually. |
| 39 | +
|
| 40 | +- Before using a shell CLI (` + "`gh`, `aws`, `kubectl`, `curl`" + `, …) or a raw HTTP API |
| 41 | + for an external service, call ` + "`mcpproxy`" + ` ` + "`retrieve_tools`" + ` with the task and |
| 42 | + the service name (e.g. "create github issue"). |
| 43 | +- Run the tools it returns with ` + "`code_execution`" + ` (JavaScript ` + "`call_tool(server, tool, args)`" + `). |
| 44 | +- If nothing relevant comes back, retry with different wording or just the |
| 45 | + service name before concluding the tool does not exist. |
| 46 | +` |
| 47 | + |
| 48 | +// routingModeCodeExecutionDisabled is a CLI-local pseudo mode: |
| 49 | +// routing_mode code_execution with enable_code_execution false. |
| 50 | +const routingModeCodeExecutionDisabled = "code_execution_disabled" |
| 51 | + |
| 52 | +const agentInstructionsCodeExecutionDisabled = `Its tools are NOT listed individually. |
| 53 | +
|
| 54 | +- Before using a shell CLI (` + "`gh`, `aws`, `kubectl`, `curl`" + `, …) or a raw HTTP API |
| 55 | + for an external service, call ` + "`mcpproxy`" + ` ` + "`retrieve_tools`" + ` with the task and |
| 56 | + the service name (e.g. "create github issue") to check whether a tool exists. |
| 57 | +- Calling those tools needs ` + "`code_execution`" + `, which is disabled in this mcpproxy |
| 58 | + config; ask the user to enable it rather than working around it. |
| 59 | +` |
| 60 | + |
| 61 | +const agentInstructionsDirect = `Each upstream tool is listed as ` + "`<server>__<tool>`" + ` (e.g. ` + "`github__create_issue`" + `). |
| 62 | +
|
| 63 | +- Before using a shell CLI (` + "`gh`, `aws`, `kubectl`, `curl`" + `, …) or a raw HTTP API |
| 64 | + for an external service, search your MCP tool list for the service name and |
| 65 | + prefer the mcpproxy tool. |
| 66 | +- Call ` + "`describe_tool`" + ` for a tool's full input schema when its listing is abbreviated. |
| 67 | +` |
| 68 | + |
| 69 | +// snippetServerName mirrors internal/server's advertisableServerName. |
| 70 | +var snippetServerName = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$`) |
| 71 | + |
| 72 | +var ( |
| 73 | + agentInstructionsWithServers bool |
| 74 | + agentInstructionsClient string |
| 75 | +) |
| 76 | + |
| 77 | +var agentInstructionsCmd = &cobra.Command{ |
| 78 | + Use: "agent-instructions", |
| 79 | + Short: "Print a CLAUDE.md / AGENTS.md snippet that makes agents use mcpproxy tools", |
| 80 | + Long: `Print a snippet for an agent's memory file (CLAUDE.md, AGENTS.md, Cursor rules) |
| 81 | +telling it that external services are reachable through mcpproxy, so it stops |
| 82 | +falling back to shell CLIs such as gh or curl. The workflow follows the |
| 83 | +configured routing_mode. |
| 84 | +
|
| 85 | +Append it to the file your client reads: |
| 86 | +
|
| 87 | + mcpproxy agent-instructions >> AGENTS.md |
| 88 | + mcpproxy agent-instructions --with-servers >> ~/.claude/CLAUDE.md |
| 89 | + mcpproxy agent-instructions --client cursor > .cursor/rules/mcpproxy.mdc |
| 90 | +
|
| 91 | +See https://docs.mcpproxy.app/features/agent-instructions`, |
| 92 | + Args: cobra.NoArgs, |
| 93 | + RunE: runAgentInstructions, |
| 94 | +} |
| 95 | + |
| 96 | +func init() { |
| 97 | + agentInstructionsCmd.Flags().BoolVar(&agentInstructionsWithServers, "with-servers", false, |
| 98 | + "List the currently connected upstream servers in the snippet (asks the running daemon; falls back to enabled servers in the config)") |
| 99 | + agentInstructionsCmd.Flags().StringVar(&agentInstructionsClient, "client", "", |
| 100 | + "Format for a specific client: claude-code, codex or cursor (cursor adds .mdc front matter)") |
| 101 | +} |
| 102 | + |
| 103 | +func runAgentInstructions(cmd *cobra.Command, _ []string) error { |
| 104 | + switch agentInstructionsClient { |
| 105 | + case "", "claude-code", "codex", "cursor": |
| 106 | + default: |
| 107 | + return fmt.Errorf("unknown --client %q (want claude-code, codex or cursor)", agentInstructionsClient) |
| 108 | + } |
| 109 | + mode := "" |
| 110 | + if cfg, err := loadUpstreamConfig(); err == nil && cfg != nil { |
| 111 | + mode = cfg.RoutingMode |
| 112 | + if mode == config.RoutingModeCodeExecution && !cfg.EnableCodeExecution { |
| 113 | + // code_execution mode with the tool switched off exposes no |
| 114 | + // calling tool at all; don't prescribe one that is absent. |
| 115 | + mode = routingModeCodeExecutionDisabled |
| 116 | + } |
| 117 | + } |
| 118 | + var servers []string |
| 119 | + if agentInstructionsWithServers { |
| 120 | + servers = agentInstructionsServers() |
| 121 | + } |
| 122 | + _, err := fmt.Fprint(cmd.OutOrStdout(), buildAgentInstructions(agentInstructionsClient, mode, servers)) |
| 123 | + return err |
| 124 | +} |
| 125 | + |
| 126 | +// buildAgentInstructions renders the snippet for client and routing mode |
| 127 | +// (unset or unknown = retrieve_tools, the default) with an optional server |
| 128 | +// list. |
| 129 | +func buildAgentInstructions(client, routingMode string, servers []string) string { |
| 130 | + var b strings.Builder |
| 131 | + if client == "cursor" { |
| 132 | + b.WriteString("---\ndescription: Use mcpproxy MCP tools for external services\nalwaysApply: true\n---\n\n") |
| 133 | + } |
| 134 | + b.WriteString(agentInstructionsHeader) |
| 135 | + switch routingMode { |
| 136 | + case config.RoutingModeDirect: |
| 137 | + b.WriteString(agentInstructionsDirect) |
| 138 | + case config.RoutingModeCodeExecution: |
| 139 | + b.WriteString(agentInstructionsCodeExecution) |
| 140 | + case routingModeCodeExecutionDisabled: |
| 141 | + b.WriteString(agentInstructionsCodeExecutionDisabled) |
| 142 | + default: |
| 143 | + b.WriteString(agentInstructionsRetrieve) |
| 144 | + } |
| 145 | + // Same rule as the MCP surface (advertisableServerName): a name that |
| 146 | + // could carry prompt text never lands in an agent memory file. |
| 147 | + var safe []string |
| 148 | + for _, name := range servers { |
| 149 | + if snippetServerName.MatchString(name) { |
| 150 | + safe = append(safe, name) |
| 151 | + } |
| 152 | + } |
| 153 | + if len(safe) > 0 { |
| 154 | + sort.Strings(safe) |
| 155 | + b.WriteString("\nServers currently behind mcpproxy: " + strings.Join(safe, ", ") + ".\n") |
| 156 | + } |
| 157 | + return b.String() |
| 158 | +} |
| 159 | + |
| 160 | +// agentInstructionsServers asks the running daemon for connected servers, |
| 161 | +// falling back to the config's enabled, non-quarantined servers. |
| 162 | +func agentInstructionsServers() []string { |
| 163 | + globalConfig, err := loadUpstreamConfig() |
| 164 | + if err != nil { |
| 165 | + fmt.Fprintf(os.Stderr, "warning: cannot load config, omitting server list: %v\n", err) |
| 166 | + return nil |
| 167 | + } |
| 168 | + logger, err := createUpstreamLogger("error") |
| 169 | + if err == nil { |
| 170 | + if client, ok := newDaemonClient(globalConfig, logger.Sugar()); ok { |
| 171 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 172 | + defer cancel() |
| 173 | + if list, err := client.GetServers(ctx); err == nil { |
| 174 | + var names []string |
| 175 | + for _, s := range list { |
| 176 | + if getBoolField(s, "connected") && !getBoolField(s, "quarantined") { |
| 177 | + names = append(names, getStringField(s, "name")) |
| 178 | + } |
| 179 | + } |
| 180 | + return names |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + fmt.Fprintln(os.Stderr, "note: daemon not reachable; listing enabled servers from the config file") |
| 185 | + var names []string |
| 186 | + for _, s := range globalConfig.Servers { |
| 187 | + if s != nil && s.Enabled && !s.Quarantined { |
| 188 | + names = append(names, s.Name) |
| 189 | + } |
| 190 | + } |
| 191 | + return names |
| 192 | +} |
0 commit comments