MyCommandMCP(注:这是一个专有名词或特定产品/系统的名称,直接翻译为中文可能无法准确传达其含义,因此保持原样。在实际应用中,如果这是一个已知的产品或系统名称,应直接使用其官方中文名称或根据上下文进行适当解释。)
一个用Rust编写的MCP(模型上下文协议)服务器,允许将系统命令作为MCP工具执行。
特点/特性
- 从可定制的YAML文件中读取工具配置
- 支持通过(指定方式)来指定配置文件
--config参数 - 安全执行系统命令
- 以JSON格式返回结果,包含状态码、输出和错误信息
- 兼容MCP协议 2024-11-05
- 支持可配置的提示模板
安装与使用
- 确保你已经安装了Rust
- 克隆或复制此项目
- 配置您的YAML工具文件
- 构建并运行:
cargo build --release运行服务器
使用默认配置文件
./target/release/mycommandmcp使用自定义配置文件
./target/release/mycommandmcp --config my-configuration.yaml查看帮助
./target/release/mycommandmcp --help配置
服务器从YAML文件中读取配置。默认情况下,它会查找 mycommand-tools.yaml 在当前目录中,但您可以指定另一个文件使用 --config 参数。
配置文件结构
配置文件支持三个主要部分: prompts, tools,和 resources。
prompts:
- name: "prompt_name"
description: "Description of what the prompt does"
content: |
Multi-line prompt template content
Can contain instructions and formatting
Supports multiple lines with proper indentation
# Alternative: Load content from a local file
- name: "file_prompt"
description: "Prompt loaded from a markdown file"
path: "/path/to/prompt.md"
# Alternative: Load content from a URL
- name: "url_prompt"
description: "Prompt loaded from a remote URL"
url: "https://example.com/prompt.md"
tools:
- name: "tool_name"
description: "Tool description for MCP"
command: "system_command"
path: "/path/where/to/execute"
accepts_args: true/false
accept_input: true/false
default_args: "default arguments string"
content_type: "mime/type" # Optional: MIME type for file responses
content_disposition: "attachment; filename=file.ext" # Optional: Content disposition
- **resources**: List of MCP resources to serve files directly by name
配置属性说明:
- 名字工具的唯一标识符
- 描述在MCP中显示的可读人类描述
- 命令执行系统的命令
- 路径命令将被执行的工作目录
- 接受参数该工具是否接受额外参数(是/否)
- 接受输入该工具是否接受通过标准输入(stdin)的输入(真/假)
- default_args(在中文中,这通常被直接保留为英文原词,但为了更贴合中文表达习惯,也可以理解为“默认参数”)(可选)默认参数始终应用于命令,在任何额外参数之前拼接
- 内容类型(可选)命令输出的MIME类型(例如,“application/pdf”,“image/png”,“text/csv”)
- 内容处置(或内容分发)(可选)内容的处理方式(例如,“attachment; filename=report.pdf”,“inline”)
资源部分
这个(或:该) resources 该部分允许您定义名为MCP的资源,这些资源直接提供文件服务。每个资源必须指定:
- 名字资源的唯一标识符
- 描述可读性的人类描述
- 路径要提供服务的文件路径
如果文件是二进制文件,服务器将自动检测正确的内容类型,并以Base64编码的数据形式返回文件。
示例资源部分
resources:
- name: "sample_text"
description: "Returns the content of a sample text file"
path: "/tmp/sample.txt"
- name: "sample_pdf"
description: "Returns a sample PDF file"
path: "/tmp/sample.pdf"
- name: "sample_image"
description: "Returns a sample PNG image"
path: "/tmp/sample.png"提示配置
这个(或“该”) prompts 该部分允许您定义可重用的提示模板。每个提示必须指定:
- 名字提示的唯一标识符
- 描述在MCP中显示的人类可读描述
并且必须选择以下内容来源中的恰好一个:
- 内容将内联提示内容作为字符串(支持多行)
|YAML 语法) - 路径包含提示内容的本地文件路径(支持Markdown文件)
- 网址用于获取提示内容的URL(支持远程Markdown文件)
示例提示部分
prompts:
- name: "summarize"
description: "Summarize a given text"
content: |
Please summarize the following text in 3 sentences or less.
Consider the main points and key details.
Maintain a clear and concise style.
- name: "code_review"
description: "Review code for best practices"
path: "/home/user/prompts/code_review.md"
- name: "translate"
description: "Translate text to Spanish"
url: "https://raw.githubusercontent.com/example/repo/main/prompts/translate.md"内容加载行为
- 内联内容直接从YAML配置文件加载
- 文件路径当请求提示时,内容会从本地文件系统读取
- URL(统一资源定位符)当请求提示时,内容会从远程URL获取
如果从文件或URL加载失败,服务器将对该特定的提示请求返回错误。
外部配置文件
MyCommandMCP支持从外部YAML文件加载额外配置,使您能够将工具、提示和资源组织在多个文件中,或从远程URL获取它们。
外部配置部分
你可以包含一个可选的 external_configs 在你的主配置文件中添加一个部分,以从外部YAML文件加载额外配置:
# Main configuration file
tools:
- name: "local_tool"
description: "A tool defined in the main config"
command: "echo"
path: "/"
accepts_args: true
accept_input: false
external_configs:
- "/path/to/external/config.yaml"
- "https://example.com/remote/config.yaml"外部文件结构
外部配置文件与主配置文件具有相同的结构。它们可以包含任意组合的 tools, prompts,和 resources 部分/章节:
# /path/to/external/tools.yaml
tools:
- name: "external_tool"
description: "A tool from an external file"
command: "ls"
path: "/"
accepts_args: true
accept_input: false
prompts:
- name: "external_prompt"
description: "A prompt from an external file"
content: "This is a prompt template from an external file"加载行为
- 文件路径相对路径是相对于主配置文件的目录来解析的
- URLs(统一资源定位符)在启动时获取远程文件并将其缓存到内存中
- 合并所有配置都被合并在一起,重复的名称会导致错误
- 验证每个外部文件都会经过验证,以确保其YAML语法正确且包含所有必需字段
从旧版本迁移
如果你使用的是单独的 tools_list, prompts_list,和 resources_list 选项,您可以迁移到统一的 external_configs 通过将所有外部文件引用合并到一个列表中来选择选项:
# Old format (deprecated)
tools_list:
- "/path/to/tools.yaml"
prompts_list:
- "/path/to/prompts.yaml"
resources_list:
- "/path/to/resources.yaml"
# New unified format
external_configs:
- "/path/to/tools.yaml"
- "/path/to/prompts.yaml"
- "/path/to/resources.yaml"- 订单外部配置按照列表中出现的顺序进行加载
用例
- 模块化配置将大型配置拆分为逻辑模块
- 团队协作通过共享文件在团队成员之间共享通用工具/提示
- 远程管理远程更新配置,无需重新部署服务器
- 版本控制将不同的工具集存放在不同的存储库中
错误处理
如果无法加载外部文件(文件未找到、网络错误、无效的YAML格式),服务器将无法启动,并显示描述性的错误信息。这确保了服务器在开始接受请求之前,所有配置都是有效的。
内容类型和文件下载
配置示例
tools:
- name: "list_files"
description: "Lists files in a specific directory"
command: "ls"
path: "/"
accepts_args: true
accept_input: false
default_args: "-l"
- name: "get_date"
description: "Gets the current system date and time"
command: "date"
path: "/"
accepts_args: false
accept_input: false
- name: "grep_text"
description: "Search for text patterns using grep from standard input"
command: "grep"
path: "/"
accepts_args: true
accept_input: true
default_args: "--color=never"\default_args\ 的工作原理:
- 当命令执行时,总是首先应用默认参数
- 如果通过MCP调用提供了额外的参数,这些参数将被附加在默认参数之后
- 例如,如果
default_args: "-l"并且你提供args: "-a",最终的命令将是:ls -l -a - 如果未提供额外参数,则仅使用默认参数
- 这个(或“该”)
default_args字段是可选的 - 如果未指定,则仅使用用户提供的参数
\accept_input\的工作原理:
- 当
accept_input: true该工具可以通过标准输入(stdin)接收文本输入 - 这对于诸如以下命令之类的操作非常有用:
grep,wc,sort等,用于处理文本流 - 输入是通过MCP提供的
input调用工具时的参数 - 带有工具的
accept_input: false不会接受任何标准输入
示例MCP工具调用:
- 仅带默认参数的工具:
{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "disk_usage", "arguments": {}}}这执行: df -h (仅使用default_args)
- 带有默认参数+额外参数的工具:
{"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "list_files", "arguments": {"args": "-a /home"}}}这执行的是: ls -l -a /home (默认参数 + 用户参数)
- 带输入的工具:
{"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "grep_text", "arguments": {"args": "pattern", "input": "line1\npattern here\nline3"}}}这执行的是: grep --color=never pattern 将输入通过管道传递到标准输入(stdin)
内容类型和文件下载
MyCommandMCP 现在支持将命令输出作为不同的内容类型返回,并具备适当的文件处理功能。这使得工具能够返回 PDF、图像、存档文件和其他二进制内容。
内容类型配置
添加可选项 content_type 并且 content_disposition 启用文件下载的字段:
tools:
- name: "generate_report"
description: "Generate a PDF report"
command: "wkhtmltopdf"
path: "/"
accepts_args: true
accept_input: false
default_args: "- /dev/stdout"
content_type: "application/pdf"
content_disposition: "attachment; filename=report.pdf"
- name: "create_backup"
description: "Create a system backup archive"
command: "tar"
path: "/"
accepts_args: true
accept_input: false
default_args: "-czf - /home"
content_type: "application/gzip"
content_disposition: "attachment; filename=backup.tar.gz"
- name: "export_data_csv"
description: "Export data as CSV file"
command: "ps"
path: "/"
accepts_args: false
accept_input: false
default_args: "aux --no-headers | awk 'BEGIN{print \"USER,PID,CPU,MEM\"} {print $1\",\"$2\",\"$3\",\"$4}'"
content_type: "text/csv"
content_disposition: "attachment; filename=processes.csv"内容类型的工作原理
- 文本内容当没有
content_type被指定或以……开头text/输出以纯文本形式返回 - 二进制内容当
content_type表示二进制内容,输出自动进行Base64编码 - 文件响应内容包含用于文件处理的适当的MIME类型和内容处置头
如需详细文档,请参阅 CONTENT_TYPES.md 翻译为中文是:内容类型.md。
MCP资源API
resources/list列出所有可用资源及其名称和描述resources/get通过名称检索特定资源的内容
示例MCP资源调用:
{"jsonrpc": "2.0", "id": 10, "method": "resources/get", "params": {"name": "sample_pdf"}}服务器将返回带有正确MIME类型和编码的文件内容。
包含的配置文件
mycommand-tools.yaml基本配置,附带示例内容类型工具mycommand-tools-extended.yaml扩展配置,包含内容类型示例
MCP协议
服务器实现了以下MCP方法:
initialize初始化服务器并返回其功能tools/list列出所有可用工具tools/call执行特定工具prompts/list列出所有可用的提示及其名称和描述prompts/get通过名称检索特定提示的完整内容
提示API
提示列表
{"jsonrpc": "2.0", "id": 1, "method": "prompts/list"}回复:
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"prompts": [
{
"name": "summarize",
"description": "Summarize a given text"
},
{
"name": "translate",
"description": "Translate text to Spanish"
}
]
}
}获取即时回复
{"jsonrpc": "2.0", "id": 2, "method": "prompts/get", "params": {"name": "summarize"}}回答:
{
"jsonrpc": "2.0",
"id": 2,
"result": {
"name": "summarize",
"description": "Summarize a given text",
"content": "Please summarize the following text in 3 sentences or less.\nConsider the main points and key details.\nMaintain a clear and concise style."
}
}响应格式
当执行一个工具时,服务器返回一个包含以下内容的JSON:
{
"status_code": 0,
"output": "command output",
"error": "errors if any"
}安全
重要这台服务器直接执行系统命令。请确保:
- 仅在受控环境中使用
- 使用安全命令配置工具
- 不要将服务器暴露在不受信任的网络中
- 仔细检查YAML配置
许可证
麻省理工学院许可证(MIT License)
任务
这个任务是使用自动脚本来帮助管理项目
发布
使用 goreleaser 生成发布版本
交互式:true
# goreleaser release --snapshot
cargo build --release
bash build-windows.sh检查
使用 clippy 和 fmt 检查代码
交互式:true
cargo check
cargo clippy
cargo fmt构建
构建项目
交互式:开启
cargo build --releasedocker: 构建并推送
构建并推送 Docker 镜像到 GitHub 容器注册表
交互式:true
docker build -t ghcr.io/sevir/mycommandmcp:latest .
docker push ghcr.io/sevir/mycommandmcp:latest
cosign sign ghcr.io/sevir/mycommandmcp