Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(cli): add coder exp mcp command #17066

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
merged 20 commits into from
Mar 31, 2025
Merged

feat(cli): add coder exp mcp command #17066

merged 20 commits into from
Mar 31, 2025

Conversation

johnstcn
Copy link
Member

@johnstcn johnstcn commented Mar 24, 2025

Adds a coder exp mcp command which will start a local MCP server listening on stdio with the following capabilities:

  • Show logged in user (coder whoami)
  • List workspaces (coder list)
  • List templates (coder templates list)
  • Start a workspace (coder start)
  • Stop a workspace (coder stop)
  • Fetch a single workspace (no direct CLI analogue)
  • Execute a command inside a workspace (coder exp rpty)
  • Report the status of a task (currently a no-op, pending task support)

This can be tested as follows:

# Start a local Coder server.
./scripts/develop.sh
# Start a workspace. Currently, creating workspaces is not supported.
./scripts/coder-dev.sh create -t docker --yes
# Add the MCP to your Claude config.
claude mcp add coder ./scripts/coder-dev.sh exp mcp
# Tell Claude to do something Coder-related. You may need to nudge it to use the tools.
claude 'start a docker workspace and tell me what version of python is installed'

NOTE: Some of this code was generated by AI tooling, but was reviewed by a human before committing.

@johnstcn johnstcn changed the title Cj/exp/mcp WIP: feat(cli): add experimental MCP server Mar 24, 2025
@johnstcn johnstcn force-pushed the cj/exp/mcp branch 2 times, most recently from 5223489 to 19a504c Compare March 28, 2025 11:26
@johnstcn johnstcn changed the title WIP: feat(cli): add experimental MCP server feat(cli): add experimental MCP server Mar 28, 2025
@johnstcn johnstcn changed the title feat(cli): add experimental MCP server feat(cli): add coder exp mcp command Mar 28, 2025
@johnstcn johnstcn marked this pull request as ready for review March 28, 2025 12:31
Copy link
Member

@mtojek mtojek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed.

I'll admit that command validation is a debatable approach, as it can be relatively easy to bypass.

if deps.Client == nil {
return nil, xerrors.New("developer error: client is required")
}
templates, err := deps.Client.Templates(ctx, codersdk.TemplateFilter{})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there risk of pagination?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, that endpoint is not paginated. There are some query parameters we could pass in, such as deleted, organization_id, exact_name etc.

defer conn.Close()

var buf bytes.Buffer
if _, err := io.Copy(&buf, conn); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the command is supposed to take more time, should we stream the response?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'd like to look into this as a future enhancement.


// TODO: Waiting on support for tasks.
deps.Logger.Info(ctx, "report task tool called", slog.F("summary", summary), slog.F("link", link), slog.F("done", done), slog.F("emoji", emoji))
/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dead code on purpose?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this if you prefer, but I was anticipating this being ready sooner.


// Example payload:
// {"jsonrpc":"2.0","id":1,"method":"tools/call", "params": {"name": "coder_workspace_exec", "arguments": {"workspace": "dev", "command": "ps -ef"}}}
func handleCoderWorkspaceExec(deps ToolDeps) mcpserver.ToolHandlerFunc {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to pass tracing data? for instance, for how long was the command running?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For whom is this data intended? For now, I've returned it in the response as JSON.

Copy link
Member

@mtojek mtojek Mar 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering if the MCP protocol has space for such metadata. If somebody wants to visualize some metrics around calls, it seems to be channel to pass all tracing info.

EDIT:
I thought about data we can display on the Tracing chart.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There doesn't seem to be, from what I can tell.

}
}

return &mcp.CallToolResult{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to return the status code? somebody may use it to build more sophisticated workflows (if commands failed, then...)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the agent will have to use the old trick of command; echo $?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let's ignore for now

Copy link
Member

@mtojek mtojek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@johnstcn johnstcn merged commit 057cbd4 into main Mar 31, 2025
30 checks passed
@johnstcn johnstcn deleted the cj/exp/mcp branch March 31, 2025 17:52
@github-actions github-actions bot locked and limited conversation to collaborators Mar 31, 2025
@stirby
Copy link
Collaborator

stirby commented Apr 1, 2025

/cherry-pick release/2.21

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants