|
| 1 | +# MCP on Ruby |
| 2 | + |
| 3 | +<div align="center"> |
| 4 | + |
| 5 | +[](https://badge.fury.io/rb/mcp_on_ruby) |
| 6 | +[](https://opensource.org/licenses/MIT) |
| 7 | + |
| 8 | +<strong>Ruby implementation of the Model Context Protocol (MCP) specification</strong> |
| 9 | +</div> |
| 10 | + |
| 11 | +## Overview |
| 12 | + |
| 13 | +The [Model Context Protocol](https://modelcontextprotocol.io) provides a standardized way for AI applications to interact with external tools and data sources. This library implements the MCP specification in Ruby, allowing developers to create both MCP servers and clients. |
| 14 | + |
| 15 | +## Features |
| 16 | + |
| 17 | +- **Full MCP 2025-03-26 Protocol Support**: Implements the latest MCP specification |
| 18 | +- **JSON-RPC 2.0 Communication**: Bidirectional messaging using the JSON-RPC standard |
| 19 | +- **Transport Options**: HTTP and STDIO transports for maximum flexibility |
| 20 | +- **Server Capabilities**: |
| 21 | + - Tools (model-controlled actions) |
| 22 | + - Resources (application-controlled context) |
| 23 | + - Prompts (user-controlled interactions) |
| 24 | + - Roots (filesystem integration) |
| 25 | +- **Client Integration**: Connect to any MCP-compatible server |
| 26 | +- **OAuth 2.1 Authentication**: Secure access to remote servers |
| 27 | +- **Streaming Support**: Real-time bidirectional communication |
| 28 | + |
| 29 | +## Installation |
| 30 | + |
| 31 | +Add this line to your application's Gemfile: |
| 32 | + |
| 33 | +```ruby |
| 34 | +gem 'mcp_on_ruby' |
| 35 | +``` |
| 36 | + |
| 37 | +And then execute: |
| 38 | + |
| 39 | +``` |
| 40 | +$ bundle install |
| 41 | +``` |
| 42 | + |
| 43 | +Or install it yourself as: |
| 44 | + |
| 45 | +``` |
| 46 | +$ gem install mcp_on_ruby |
| 47 | +``` |
| 48 | + |
| 49 | +## Quick Start: Server |
| 50 | + |
| 51 | +Create a simple MCP server with tools: |
| 52 | + |
| 53 | +```ruby |
| 54 | +require 'mcp_on_ruby' |
| 55 | + |
| 56 | +# Create a server with tools |
| 57 | +server = MCP::Server.new do |s| |
| 58 | + # Define a tool |
| 59 | + s.tool "weather.get_forecast" do |params| |
| 60 | + location = params[:location] |
| 61 | + # ... fetch weather data ... |
| 62 | + { forecast: "Sunny", temperature: 72, location: location } |
| 63 | + end |
| 64 | + |
| 65 | + # Add a resource |
| 66 | + s.resource "user.profile" do |
| 67 | + { name: "John", email: "[email protected]" } |
| 68 | + end |
| 69 | +end |
| 70 | + |
| 71 | +# Start the server |
| 72 | +server.start |
| 73 | +``` |
| 74 | + |
| 75 | +## Quick Start: Client |
| 76 | + |
| 77 | +Connect to an MCP server and use its tools: |
| 78 | + |
| 79 | +```ruby |
| 80 | +require 'mcp_on_ruby' |
| 81 | + |
| 82 | +# Create a client |
| 83 | +client = MCP::Client.new(url: "http://localhost:3000") |
| 84 | + |
| 85 | +# Connect to the server |
| 86 | +client.connect |
| 87 | + |
| 88 | +# List available tools |
| 89 | +tools = client.list_tools |
| 90 | +puts tools |
| 91 | + |
| 92 | +# Call a tool |
| 93 | +result = client.call_tool("weather.get_forecast", location: "San Francisco") |
| 94 | +puts result.inspect |
| 95 | + |
| 96 | +# Get a resource |
| 97 | +profile = client.get_resource("user.profile") |
| 98 | +puts profile.inspect |
| 99 | + |
| 100 | +# Disconnect |
| 101 | +client.disconnect |
| 102 | +``` |
| 103 | + |
| 104 | +## Architecture |
| 105 | + |
| 106 | +MCP on Ruby follows the MCP specification's architecture: |
| 107 | + |
| 108 | +1. **Protocol Layer**: JSON-RPC 2.0 communication over multiple transports |
| 109 | +2. **Server**: Exposes tools, resources, and prompts to clients |
| 110 | +3. **Client**: Connects to servers and uses their capabilities |
| 111 | +4. **Authentication**: OAuth 2.1 for secure remote connections |
| 112 | + |
| 113 | +## Advanced Usage |
| 114 | + |
| 115 | +See the [wiki](https://github.com/nagstler/mcp_on_ruby/wiki) for advanced usage, including: |
| 116 | + |
| 117 | +- Creating complex tool hierarchies |
| 118 | +- Implementing custom authentication |
| 119 | +- Streaming responses |
| 120 | +- Working with resources and prompts |
| 121 | +- File system integration with roots |
| 122 | + |
| 123 | +## Development |
| 124 | + |
| 125 | +After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. |
| 126 | + |
| 127 | +## Contributing |
| 128 | + |
| 129 | +Bug reports and pull requests are welcome on GitHub at https://github.com/nagstler/mcp_on_ruby. |
| 130 | + |
| 131 | +## License |
| 132 | + |
| 133 | +The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). |
0 commit comments