mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: add Coder Agents chat tools to the MCP toolsdk (#28025)
Exposes the experimental Coder Agents chats API through the MCP tool registry, so MCP clients (the hosted `/api/experimental/mcp/http` server and `coder exp mcp server`) can start and drive server-side coding agents. New tools in `codersdk/toolsdk`, all thin wrappers over existing `codersdk.ExperimentalClient` methods: | Tool | Wraps | |---|---| | `coder_create_chat` | `CreateChat` (prompt, optional org, model config, labels) | | `coder_get_chat` | `GetChat` (status, last error, last turn summary, workspace, files) | | `coder_get_chat_messages` | `GetChatMessages` (user-facing parts, chronological, cursor pagination, queued prompts) | | `coder_send_chat_message` | `CreateChatMessage` (queue or interrupt busy behavior) | | `coder_interrupt_chat` | `InterruptChat` | | `coder_archive_chat` | `UpdateChat` with `archived: true` | | `coder_list_chat_model_configs` | `ListChatModelConfigs` (enabled configs with default flag) | Both MCP servers register tools from `toolsdk.All`, so no additional wiring is needed. Responses are trimmed to what an MCP caller needs (IDs as strings, user-facing transcripts) rather than full SDK payloads. No new endpoints and no database changes. Also adds MCP [prompts](https://modelcontextprotocol.io/specification/2026-07-28/server/prompts) for the chat workflows, defined once in `codersdk/toolsdk` and registered by both servers: | Prompt | Purpose | |---|---| | `coder_agents_delegate` | delegate a task to a Coder Agents chat and monitor it to completion | | `coder_agents_check` | check the status and recent activity of an existing chat | Each prompt declares the tools its workflow needs; the stdio server skips prompts whose tools are excluded by `--allowed-tools`. Tests run the tools against a chat-enabled coderdtest instance (fake OpenAI-compatible provider plus in-process AI bridge), covering the full lifecycle, an interrupt against a blocked turn, pagination cursors, permission-dependent model config filtering, and argument validation. Prompt coverage spans SDK rendering, the hosted `prompts/list`/`prompts/get` round trip, and the stdio server including allowlist gating. > Mux created this PR on Mike's behalf.
This commit is contained in:
@@ -101,6 +101,13 @@ func (s *Server) RegisterTools(client *codersdk.Client, opts ...func(*toolsdk.De
|
||||
return nil
|
||||
}
|
||||
|
||||
// RegisterPrompts registers all MCP prompt templates with the server.
|
||||
func (s *Server) RegisterPrompts() {
|
||||
for _, prompt := range toolsdk.AllPrompts {
|
||||
RegisterSDKPrompt(s.mcpServer, prompt)
|
||||
}
|
||||
}
|
||||
|
||||
// ChatGPT tools are the search and fetch tools as defined in https://platform.openai.com/docs/mcp.
|
||||
// We do not expose any extra ones because ChatGPT has an undocumented "Safety Scan" feature.
|
||||
// In my experiments, if I included extra tools in the MCP server, ChatGPT would often - but not always -
|
||||
@@ -165,6 +172,34 @@ func RegisterSDKTool(srv *mcp.Server, sdkTool toolsdk.GenericTool, tb toolsdk.De
|
||||
})
|
||||
}
|
||||
|
||||
// RegisterSDKPrompt registers a [toolsdk.Prompt] with an MCP server.
|
||||
func RegisterSDKPrompt(srv *mcp.Server, sdkPrompt toolsdk.Prompt) {
|
||||
args := make([]*mcp.PromptArgument, 0, len(sdkPrompt.Arguments))
|
||||
for _, arg := range sdkPrompt.Arguments {
|
||||
args = append(args, &mcp.PromptArgument{
|
||||
Name: arg.Name,
|
||||
Description: arg.Description,
|
||||
Required: arg.Required,
|
||||
})
|
||||
}
|
||||
srv.AddPrompt(&mcp.Prompt{
|
||||
Name: sdkPrompt.Name,
|
||||
Description: sdkPrompt.Description,
|
||||
Arguments: args,
|
||||
}, func(_ context.Context, req *mcp.GetPromptRequest) (*mcp.GetPromptResult, error) {
|
||||
text, err := sdkPrompt.Render(req.Params.Arguments)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &mcp.GetPromptResult{
|
||||
Description: sdkPrompt.Description,
|
||||
Messages: []*mcp.PromptMessage{
|
||||
{Role: "user", Content: &mcp.TextContent{Text: text}},
|
||||
},
|
||||
}, nil
|
||||
})
|
||||
}
|
||||
|
||||
type slogHandler struct {
|
||||
logger slog.Logger
|
||||
}
|
||||
|
||||
@@ -96,6 +96,31 @@ func TestMCPHTTP_E2E_ClientIntegration(t *testing.T) {
|
||||
|
||||
// Check for some basic tools that should be available
|
||||
assert.Contains(t, foundTools, toolsdk.ToolNameGetAuthenticatedUser, "Should have authenticated user tool")
|
||||
|
||||
prompts, err := mcpClient.ListPrompts(ctx, nil)
|
||||
require.NoError(t, err)
|
||||
var foundPrompts []string
|
||||
for _, prompt := range prompts.Prompts {
|
||||
foundPrompts = append(foundPrompts, prompt.Name)
|
||||
}
|
||||
for _, prompt := range toolsdk.AllPrompts {
|
||||
require.Contains(t, foundPrompts, prompt.Name)
|
||||
}
|
||||
|
||||
promptResult, err := mcpClient.GetPrompt(ctx, &mcp.GetPromptParams{
|
||||
Name: toolsdk.PromptNameAgentsDelegate,
|
||||
Arguments: map[string]string{"task": "Fix the flaky test."},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, promptResult.Messages, 1)
|
||||
require.Equal(t, mcp.Role("user"), promptResult.Messages[0].Role)
|
||||
promptText, ok := promptResult.Messages[0].Content.(*mcp.TextContent)
|
||||
require.True(t, ok)
|
||||
require.Contains(t, promptText.Text, "Fix the flaky test.")
|
||||
require.Contains(t, promptText.Text, toolsdk.ToolNameCreateChat)
|
||||
|
||||
_, err = mcpClient.GetPrompt(ctx, &mcp.GetPromptParams{Name: toolsdk.PromptNameAgentsDelegate})
|
||||
require.ErrorContains(t, err, "missing required prompt argument: task")
|
||||
require.NotNil(t, userTool)
|
||||
require.NotNil(t, writeFileTool)
|
||||
require.NotNil(t, userTool.Annotations)
|
||||
|
||||
@@ -80,6 +80,7 @@ func (api *API) mcpHTTPHandler() http.Handler {
|
||||
if err := mcpServer.RegisterTools(authenticatedClient, toolOpt); err != nil {
|
||||
api.Logger.Warn(r.Context(), "failed to register MCP tools", slog.Error(err))
|
||||
}
|
||||
mcpServer.RegisterPrompts()
|
||||
case MCPToolsetChatGPT:
|
||||
if err := mcpServer.RegisterChatGPTTools(authenticatedClient, toolOpt); err != nil {
|
||||
api.Logger.Warn(r.Context(), "failed to register MCP tools", slog.Error(err))
|
||||
|
||||
Reference in New Issue
Block a user