JSON-RPC服务器工具

MCP Crystal Shard是一个轻量级的、可重用的Crystal实现,用于构建暴露工具给AI助手的JSON-RPC服务器。

  • by ralsina
  • Crystal
  • 本地部署
  • 8 星标
  • 更新于

使用说明

MCP晶体碎片

模型上下文协议(MCP)的轻量级、可重用的Crystal实现,用于构建JSON-RPC服务器,向AI助手公开工具。

特性

  • 工具注册表:通过基于宏的继承自动注册工具
  • JSON-RPC 2.0:完全符合MCP协议规范
  • 服务器发送的事件:实时通信支持
  • 依赖注入:通过身份验证、日志记录和配置接口实现干净的分离
  • 凯末尔一体化:易于与Kemal web框架集成

快速开始

添加到您的 shard.yml:

dependencies:
  mcp:
    github: ralsina/mcp

简单的stdio MCP服务器

require "mcp"

# Define your tool by inheriting from AbstractTool
class MyTool < MCP::AbstractTool
  @@tool_name = "my_tool"
  @@tool_description = "A simple example tool that returns 42"
  @@tool_input_schema = {
    "type"       => "object",
    "properties" => {} of String => String,
  }.to_json

  def invoke(params : Hash(String, JSON::Any), env : HTTP::Server::Context? = nil)
    {
      "result" => 42
    }
  end
end

# Start a simple stdio MCP server
MCP::StdioHandler.start_server

就是这样!您的MCP服务器已准备好通过stdin/stdout处理请求。

Web服务器集成

require "kemal"
require "mcp"

# Create your tools
class EchoTool < MCP::AbstractTool
  @@tool_name = "echo"
  @@tool_description = "Echoes back the input message"
  @@tool_input_schema = {
    "type"       => "object",
    "properties" => {
      "message" => {
        "type"        => "string",
        "description" => "Message to echo back",
      },
    },
    "required" => ["message"],
  }.to_json

  def invoke(params : Hash(String, JSON::Any), env : HTTP::Server::Context? = nil)
    message = params["message"]?.try(&.as_s) || "no message"
    {
      "echo" => message
    }
  end
end

# Set up MCP server
mcp_server = MCP::Server.new

# Kemal routes
post "/mcp" do |env|
  # Simple user ID extraction (implement your own auth)
  user_id = env.request.headers["User-Id"]? || "anonymous"

  response = mcp_server.handle_request(env, user_id)
  env.response.content_type = "application/json"
  response
end

get "/mcp" do |env|
  user_id = env.request.headers["User-Id"]? || "anonymous"
  mcp_server.handle_sse(env, user_id)
end

Kemal.run

建筑

分片提供了以下核心组件:

  • MCP::AbstractTool:所有具有自动注册功能的工具的基类
  • MCP::Server:处理JSON-RPC请求和SSE连接
  • MCP::Handler:Kemal集成层
  • 接口类:用于身份验证、日志记录和配置注入

工具注册

定义类时,工具会自动注册:

class MyTool < MCP::AbstractTool
  # Tool metadata
  @@tool_name = "my_tool"
  @@tool_description = "Does something useful"
  @@tool_input_schema = {
    "type" => "object",
    "properties" => {
      "input" => {
        "type" => "string",
        "description" => "Input parameter",
      },
    },
    "required" => ["input"],
  }.to_json

  # Implementation
  def invoke(params : Hash(String, JSON::Any), env : HTTP::Server::Context? = nil)
    # Your tool logic here
    input_value = params["input"]?.try(&.as_s) || ""
    {
      "result" => "processed: #{input_value}"
    }
  end
end

协议支持

  • JSON-RPC 2.0:请求/响应的核心协议
  • MCP 2024-11-05:标准协议版本
  • 工具/列表:列出带有元数据的可用工具
  • 工具/电话:使用参数执行工具
  • 服务器发送的事件:实时更新和流媒体

例子

看 examples/ 完整工作示例目录:

基本示例(examples/basic/)

  • 简单的stdio MCP服务器
  • 演示核心工具注册和JSON-RPC处理
  • 非常适合命令行MCP服务器

Web示例(examples/web/)

  • 基于Kemal框架的Web MCP服务器
  • HTTP JSON-RPC端点和服务器发送事件
  • 演示身份验证、日志记录和HTTP上下文访问
  • 包括用于测试的内置web界面

许可证

MIT许可证-有关详细信息,请参阅许可证文件。