MCP ExplorerExplorer

Cobramcp

@avtionon 23 days ago
1 MIT
FreeCommunity
AI Systems
#mcp#cobra#model-context-protocol
CobraMcp is a Go language toolkit for converting Cobra command-line applications into the Tool format of the Model Context Protocol (MCP).

Overview

What is Cobramcp

CobraMcp is a Go language library designed to convert Cobra command-line applications into the Tool format of the Model Context Protocol (MCP).

Use cases

Use cases for CobraMcp include building command-line tools that need to communicate with MCP services, integrating existing Cobra applications into a streaming architecture, and enhancing server capabilities with command-line functionalities.

How to use

To use CobraMcp, you can install it via Go by running ‘go get -u github.com/avtion/cobramcp@latest’. You then create a Cobra command and generate MCP tools using the provided functions.

Key features

Key features of CobraMcp include the ability to integrate Cobra commands into MCP services, compatibility with the ThinkInAIXYZ/go-mcp library, and the ability to generate MCP tools programmatically.

Where to use

CobraMcp can be used in any application that requires command-line interfaces to interact with Model Context Protocol (MCP) services, particularly in server environments.

Content

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

Tools

No tools

Comments

Recommend MCP Servers

View All MCP Servers