MCP-RB
一个轻量级的Ruby框架,用于使用类似Sinatra的DSL实现MCP(模型上下文协议)服务器。
安装
将此行添加到应用程序的Gemfile中:
gem 'mcp-rb'用法
下面是一个如何创建MCP服务器的简单示例:
require 'mcp'
name "hello-world"
version "1.0.0"
# Define a resource
resource "hello://world" do
name "Hello World"
description "A simple hello world message"
call { "Hello, World!" }
end
# Define a resource template
resource_template "hello://{user_name}" do
name "Hello User"
description "A simple hello user message"
call { |args| "Hello, #{args[:user_name]}!" }
end
# Define a tool
tool "greet" do
description "Greet someone by name"
argument :name, String, required: true, description: "Name to greet"
call do |args|
"Hello, #{args[:name]}!"
end
end
# Define a tool with nested arguments
tool "greet_full_name" do
description "Greet someone by their full name"
argument :person, required: true, description: "Person to greet" do
argument :first_name, String, required: false, description: "First name"
argument :last_name, String, required: false, description: "Last name"
end
call do |args|
"Hello, First: #{args[:person][:first_name]} Last: #{args[:person][:last_name]}!"
end
end
# Define a tool with an Array argument
tool "group_greeting" do
description "Greet multiple people at once"
argument :people, Array, required: true, items: String, description: "People to greet"
call do |args|
args[:people].map { |person| "Hello, #{person}!" }.join(", ")
end
end支持的规格
参考: MCP 2024-11-05
- 拼 - stdio传输
- 资源 - 资源/读取 - 资源/列表 - 资源/模板/列表 - 工具 - 工具/列表 - 工具/调用
目前还不支持任何功能。
测试
rake test
rake test:unit # run only the unit tests, skipping tests that test a running server使用MCP检查员进行测试
bunx @modelcontextprotocol/inspector $(pwd)/examples/hello_world.rb格式化
bundle exec standardrb --fix您还可以使用rake任务:
rake lint # Run standardrb to check code style
rake lint:fix # Auto-fix standardrb issues发布
要发布新版本,请执行以下操作:
- 更新版本
lib/mcp/version.rb - 更新
CHANGELOG.md - 创建一个git标签
git add .
git commit -m "Release vx.y.z"
git tag vx.y.z
git push --tags- 构建并推广RubyGems
gem build mcp-rb.gemspec
gem push mcp-rb-*.gem更新日志
看 更改日志.md
