cobramcp
CobraMcp 是一个 Go 语言工具库,用于将 Cobra 命令行应用程序转换为 Model Context Protocol (MCP) 的 Tool 格式。
claude mcp add --transport stdio avtion-cobramcp go run ./...
How to use
CobraMcp is a Go-based utility that converts Cobra command trees into MCP Tools, allowing you to expose Cobra-based functionality as MCP tools in a Streamable MCP server. The library scans your Cobra commands, generates MCP Tool definitions, and registers them with the MCP server so clients can discover and invoke commands via the MCP protocol. The example shows how to set up a minimal HTTP-based MCP server and automatically expose a simple Cobra command (e.g., an add tool that sums two numbers) as an MCP tool. You’ll typically integrate this into your existing Go service, using the generated Tool and ToolHandler to register with the MCP server and expose an HTTP transport endpoint for MCP requests.
How to install
Prerequisites:
- Go installed (1.20+ recommended)
- A Go module for your project
Install CobraMcp:
- Initialize your module if you haven’t already: go mod init your-module-name
- Add CobraMcp as a dependency: go get -u github.com/avtion/cobramcp@latest
- Build your project as usual: go build ./...
Usage outline:
- Import the library and Cobra in your main.go, generate MCP tools from your Cobra commands, and register them with the MCP server as shown in the README example. Then start your HTTP transport (or any supported MCP transport) and expose the /mcp endpoint for clients.
Example commands:
-
Generate MCP tools from a Cobra command tree and register with the MCP server: tools, err := cobramcp.GenerateMcpTools(newExampleCommand, cobramcp.Option{}) if err != nil { panic(err) } for name := range tools.Tools { tool := tools.Tools[name] s.RegisterTool(tool.Tool, tool.ToolHandler) }
-
Start an HTTP MCP transport endpoint (as shown in the README): router := http.NewServeMux() router.HandleFunc("/mcp", mcpHandler.HandleMCP().ServeHTTP) httpServer := &http.Server{ Addr: ":8080", Handler: router, IdleTimeout: time.Minute } go func() { s.Run() }() httpServer.ListenAndServe()
Additional notes
Notes:
- The CobraMcp tool is designed to integrate Cobra-based command trees into MCP as Tools. You’ll typically generate Tools from your Cobra commands and register them with an MCP server at runtime.
- The example uses a Streamable HTTP transport from the go-mcp library, but you can adapt to other MCP transports supported by the library.
- If you run into type or registration errors, ensure your Cobra commands are properly constructed (e.g., proper Use and Run/RunE functions) since the MCP Tool generation relies on the Cobra command structure.
- Ensure your environment exposes the /mcp endpoint if you’re deploying behind proxies or firewalls; appropriate CORS/headers may be needed depending on your deployment.
- This project is a library; there is no standalone binary in the README, so you typically embed the generation and server registration in your Go service.
Related MCP Servers
mcp-language
mcp-language-server gives MCP enabled clients access semantic tools like get definition, references, rename, and diagnostics.
sandbox
A Model Context Protocol (MCP) server that enables LLMs to run ANY code safely in isolated Docker containers.
mcp-shell
Give hands to AI. MCP server to run shell commands securely, auditably, and on demand.
hop
Fast, elegant SSH connection manager with a TUI dashboard and MCP server
mcp-kit
MCP (Model Context Protocol) Kit for Go - A Complete MCP solutions for ready to use
mcp4go
A comprehensive Go SDK for the Model Context Protocol (MCP) - simplifying AI application development by abstracting away protocol complexities.