MCPInstall
cobramcp logo

cobramcp

by avtion 1 stars avtion/cobramcp

CobraMcp is a Go language toolkit for converting Cobra command-line applications into the Tool format of the Model Context Protocol (MCP).

READMEmain

CobraMcp

Overview

CobraMcp 是一个 Go 语言工具库,用于将 Cobra 命令行应用程序转换为 Model Context Protocol (MCP) 的 Tool 格式。

思路和部分代码来源于 mcp-cobra.

与 mcp-cobra 不同,CobraMcp 是基于 ThinkInAIXYZ/go-mcp 实现。

你可以通过函数生成的 MCP Tool 集成入你的 SSE / Streamable MCP 服务。

Quick Start

go get -u github.com/avtion/cobramcp@latest

Example

package main

import (
	"errors"
	"github.com/ThinkInAIXYZ/go-mcp/server"
	"github.com/ThinkInAIXYZ/go-mcp/transport"
	"github.com/avtion/cobramcp"
	"github.com/spf13/cobra"
	"net/http"
	"time"
)

func newExampleCommand() *cobra.Command {
	c := &cobra.Command{Use: "example"}
	cAdd := &cobra.Command{
		Use:   "add",
		Short: "Add two numbers",
	}
	cAdd.Flags().Int("a", 0, "First number")
	cAdd.Flags().Int("b", 0, "Second number")
	cAdd.RunE = func(cmd *cobra.Command, args []string) error {
		a, _ := cmd.Flags().GetInt("a")
		b, _ := cmd.Flags().GetInt("b")
		result := a + b
		cmd.Printf("Result: %v", result)
		return nil
	}
	c.AddCommand(cAdd)
	return c
}

func main() {
	tp, mcpHandler, err := transport.NewStreamableHTTPServerTransportAndHandler()
	if err != nil {
		panic(err)
	}
	s, err := server.NewServer(tp)
	if err != nil {
		panic(err)
	}
	tools, err := cobramcp.GenerateMcpTools(newExampleCommand, cobramcp.Option{})
	if err != nil {
		panic(err)
	}
	for name := range tools.Tools {
		tool := tools.Tools[name]
		// Setup cobra command mcp
		s.RegisterTool(tool.Tool, tool.ToolHandler)
	}

	// this example from https://github.com/ThinkInAIXYZ/go-mcp/blob/main/examples/http_handler/main.go
	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()
}

License

MIT License

More Developer Tools MCP servers

Similar servers from the same category.

Sourcebot

sourcebot-dev

Enables code search across multiple repository hosts including GitHub, GitLab, Gitea, Gerrit, and Bitbucket with advanced filtering options for exploring large codebases through natural language queries.

searchgit

CodeAlive

codealive-ai

Provides a bridge to CodeAlive's platform for semantic code search, repository exploration, and context-aware chat completions that leverage deep understanding of entire codebases including documentation and dependencies.

search

Provides a secure, sandboxed environment for executing Python, Node.js, and shell commands in ephemeral containers with file management capabilities for code testing, visualization, and data analysis.

filesystemdatabase

NovaCV

lightbaby

Integrates with NovaCV API to generate, analyze, and convert resumes in various formats without dealing with complex document formatting

PDFSupport+5