Cover image
Try Now
2025-04-14

3 years

Works with Finder

0

Github Watches

0

Github Forks

0

Github Stars

MCP-Cobra

MCP-Cobra is a Go library that integrates the Cobra command-line interface framework with the Model Context Protocol (MCP). This library allows you to expose your Cobra CLI commands as MCP tools, making them accessible to AI assistants and other MCP clients.

Features

  • Automatically convert Cobra commands to MCP tools
  • Support for various flag types (string, int, bool, float)
  • Seamless integration with existing Cobra-based CLIs
  • Run your CLI application in traditional command-line mode or as an MCP server

Installation

go get github.com/PlusLemon/mcp-cobra

Usage

To use MCP-Cobra, you need to:

  1. Create a Cobra command structure as usual
  2. Initialize an MCP server with your root command
  3. Serve the MCP server via stdio when needed

Example:

package main

import (
    "fmt"
    "os"

    "github.com/PlusLemon/mcp-cobra/mcp"
    "github.com/spf13/cobra"
)

func main() {
    rootCmd := &cobra.Command{
        Use:   "myapp",
        Short: "My application description",
    }

    // Add your commands and flags...

    // Check if the application should run as an MCP server
    if len(os.Args) > 1 && os.Args[1] == "mcp-server" {
        mcpServer := mcp.NewMCPServer(rootCmd)
        if err := mcpServer.ServeStdio(); err != nil {
            fmt.Printf("MCP server error: %v\n", err)
            os.Exit(1)
        }
    } else {
        // Run as a normal CLI application
        if err := rootCmd.Execute(); err != nil {
            fmt.Println(err)
            os.Exit(1)
        }
    }
}

Example: foocli

The foocli directory contains a complete working example of MCP-Cobra in action.

What it does

foocli is a simple CLI application with a greet command that prints a greeting message. It can run both as a traditional CLI and as an MCP server.

Running the example

As a CLI:

# Build the example
go build -o bin/foo ./foocli

# Run as a regular CLI
./bin/foo greet --name "World"
# Output: Hello, World!

As an MCP server:

# Run as an MCP server
./bin/foo mcp-server

When running as an MCP server, the greet command is exposed as an MCP tool that can be invoked by MCP clients.

Example Code

package main

import (
	"fmt"
	"os"

	"github.com/PlusLemon/mcp-cobra/mcp"
	"github.com/spf13/cobra"
)

func main() {
	rootCmd := &cobra.Command{
		Use:   "foo",
		Short: "Foo Demo CLI",
	}

	// Define subcommand
	var greetWord string
	greetCmd := &cobra.Command{
		Use:   "greet",
		Short: "Greet someone",
		Run: func(cmd *cobra.Command, args []string) {
			fmt.Printf("Hello, %s!\n", greetWord)
		},
	}
	greetCmd.Flags().StringVar(&greetWord, "name", "Foo", "Name to greet")

	rootCmd.AddCommand(greetCmd)

	if len(os.Args) > 1 && os.Args[1] == "mcp-server" {
		mcpServer := mcp.NewMCPServer(rootCmd)
		if err := mcpServer.ServeStdio(); err != nil {
			fmt.Printf("MCP server error: %v\n", err)
			os.Exit(1)
		}
	} else {
		if err := rootCmd.Execute(); err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
	}
}

How It Works

When running in MCP server mode, MCP-Cobra analyzes your Cobra command structure and converts each command to an MCP tool. Command flags are converted to tool parameters with appropriate types and metadata.

When an MCP client calls a tool, MCP-Cobra:

  1. Maps the tool call to the appropriate Cobra command
  2. Converts the tool parameters to command flags
  3. Executes the command
  4. Captures the command output
  5. Returns the result to the MCP client

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

相关推荐

  • av
  • 毫不费力地使用一个命令运行LLM后端,API,前端和服务。

  • 1Panel-dev
  • 🔥1Panel提供了直观的Web接口和MCP服务器,用于在Linux服务器上管理网站,文件,容器,数据库和LLMS。

  • WangRongsheng
  • 🧑‍🚀 llm 资料总结(数据处理、模型训练、模型部署、 o1 模型、mcp 、小语言模型、视觉语言模型)|摘要世界上最好的LLM资源。

  • rulego
  • ⛓️Rulego是一种轻巧,高性能,嵌入式,下一代组件编排规则引擎框架。

  • Byaidu
  • PDF科学纸翻译带有保留格式的pdf -基于ai完整保留排版的pdf文档全文双语翻译

  • n8n-io
  • 具有本机AI功能的公平代码工作流程自动化平台。将视觉构建与自定义代码,自宿主或云相结合,400+集成。

  • Onelevenvy
  • Flock是一个基于工作流程的低音平台,可快速构建聊天机器人,抹布和协调多代理团队,由Langgraph,Langchain,Langchain,Fastapi和Nextjs提供支持。(羊群工作流工作流的低代码平台,rag rag rag 用于快速构建聊天机器人、 rag temant Agent fastem temantfaster和muti-agent agagent应用

  • sigoden
  • 使用普通的bash/javascript/python函数轻松创建LLM工具和代理。

  • RockChinQ
  • 😎简单易用、🧩丰富生态 -大模型原生即时通信机器人平台| 适配QQ / 微信(企业微信、个人微信) /飞书 /钉钉 / discord / telegram / slack等平台| 支持chatgpt,deepseek,dify,claude,基于LLM的即时消息机器人平台,支持Discord,Telegram,微信,Lark,Dingtalk,QQ,Slack

  • hkr04
  • 轻巧的C ++ MCP(模型上下文协议)SDK

  • dmayboroda
  • 带有可配置容器的本地对话抹布

    Reviews

    3 (6)
    Avatar
    user_MW1lzfsi
    2025-04-24

    mcp-cobra by PlusLemon is an outstanding application that has revolutionized my workflow. Its intuitive interface and robust features make it incredibly easy to manage tasks efficiently. If you're looking for a reliable and user-friendly tool, this is a game-changer. Highly recommended!

    Avatar
    user_05VprB86
    2025-04-24

    I've been using mcp-cobra by PlusLemon for a while, and it's been a game-changer. The seamless integration and user-friendly interface make it a standout product. It caters to all my needs efficiently and effectively, enhancing my productivity. Highly recommend!

    Avatar
    user_NaMDItNm
    2025-04-24

    As a loyal user of mcp applications, I can confidently say that mcp-cobra by PlusLemon stands out remarkably. Its seamless interface and powerful tools have boosted my productivity significantly. The user-friendly experience coupled with its sophisticated features make mcp-cobra a top-notch application in its category. Highly recommended for anyone looking to streamline their workflow effectively!

    Avatar
    user_HA9FC7Dv
    2025-04-24

    I've been using the mcp-cobra application by PlusLemon, and it's simply fantastic! The interface is user-friendly and intuitive, making it easy to navigate through different features. It's clear that a lot of thought was put into the design and functionality. Highly recommend it to anyone looking for a reliable and efficient tool!

    Avatar
    user_ifAwrK9D
    2025-04-24

    As a devoted user of the mcp application, I am thoroughly impressed with mcp-cobra by PlusLemon. It’s intuitive, fast, and extremely efficient. From the moment you start using it, the welcoming message sets the tone for a seamless experience. The product link and start URL are straightforward, making navigation easy. Highly recommend if you're looking for quality and performance!

    Avatar
    user_JkePHQxI
    2025-04-24

    As a dedicated user of MCP applications, I am thrilled with the performance of mcp-cobra! PlusLemon has truly outdone themselves with this product. The interface is user-friendly and the functionality is top-notch. It has significantly improved my workflow. Highly recommended for anyone looking to enhance their productivity!