mcp_on_ruby 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +13 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/CONTRIBUTING.md +92 -0
- data/LICENSE.txt +21 -0
- data/README.md +491 -0
- data/lib/ruby_mcp/configuration.rb +39 -0
- data/lib/ruby_mcp/errors.rb +17 -0
- data/lib/ruby_mcp/models/context.rb +51 -0
- data/lib/ruby_mcp/models/engine.rb +31 -0
- data/lib/ruby_mcp/models/message.rb +60 -0
- data/lib/ruby_mcp/providers/anthropic.rb +269 -0
- data/lib/ruby_mcp/providers/base.rb +57 -0
- data/lib/ruby_mcp/providers/openai.rb +265 -0
- data/lib/ruby_mcp/schemas.rb +56 -0
- data/lib/ruby_mcp/server/app.rb +84 -0
- data/lib/ruby_mcp/server/base_controller.rb +49 -0
- data/lib/ruby_mcp/server/content_controller.rb +68 -0
- data/lib/ruby_mcp/server/contexts_controller.rb +63 -0
- data/lib/ruby_mcp/server/controller.rb +29 -0
- data/lib/ruby_mcp/server/engines_controller.rb +34 -0
- data/lib/ruby_mcp/server/generate_controller.rb +140 -0
- data/lib/ruby_mcp/server/messages_controller.rb +30 -0
- data/lib/ruby_mcp/server/router.rb +84 -0
- data/lib/ruby_mcp/storage/base.rb +43 -0
- data/lib/ruby_mcp/storage/memory.rb +69 -0
- data/lib/ruby_mcp/validator.rb +45 -0
- data/lib/ruby_mcp/version.rb +6 -0
- data/lib/ruby_mcp.rb +52 -0
- metadata +274 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cc959666bde188d8d9642e81879d3e16436b592a36e503e7c3d6f23dd040810b
|
4
|
+
data.tar.gz: ce86ed0d4ce97276435ad7192133dc8a6b613cc4974996cdd41cbabae1f15ae7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a41b4314669cf296ff64d5d71524472da25de3ca43e8b27bfab3d1b5fdf5e2969f208c11c5b094849b648f7e1bc53ce3b51bf7c4917e4322542d026d53488d28
|
7
|
+
data.tar.gz: 541c892381b491280fa0c74f168782afcba411d43657686880cabd67a560a1fc34b7f8529ce0049cf3ac477209b52e134f60dcd3083aab10faa3dee311341803
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## [0.1.0] - 2025-04-18
|
4
|
+
|
5
|
+
Initial release of version 0.1.0 of the Ruby Gem.
|
6
|
+
|
7
|
+
### Added
|
8
|
+
- Core MCP implementation with Rack server
|
9
|
+
- Provider support for OpenAI and Anthropic
|
10
|
+
- In-memory storage backend
|
11
|
+
- Context, message, and content management
|
12
|
+
- Basic authentication support
|
13
|
+
- Comprehensive test suite
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at nagendra.dhanakeerthi@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# Contributing to RubyMCP
|
2
|
+
|
3
|
+
Thank you for considering contributing to RubyMCP! This document outlines the process for contributing to the project.
|
4
|
+
|
5
|
+
## Code of Conduct
|
6
|
+
|
7
|
+
This project and everyone participating in it is governed by our [Code of Conduct](CODE_OF_CONDUCT.md). By participating, you are expected to uphold this code.
|
8
|
+
|
9
|
+
## How Can I Contribute?
|
10
|
+
|
11
|
+
### Reporting Bugs
|
12
|
+
|
13
|
+
This section guides you through submitting a bug report for RubyMCP.
|
14
|
+
|
15
|
+
Before creating bug reports, please check [the issue list](https://github.com/nagstler/mcp_on_ruby/issues) to avoid duplicating an existing report. When you create a bug report, include as many details as possible:
|
16
|
+
|
17
|
+
* **Use a clear and descriptive title**
|
18
|
+
* **Describe the exact steps to reproduce the bug**
|
19
|
+
* **Provide specific examples**
|
20
|
+
* **Describe the behavior you observed**
|
21
|
+
* **Explain the behavior you expected**
|
22
|
+
* **Include screenshots or animated GIFs** if possible
|
23
|
+
* **Include details about your configuration and environment**
|
24
|
+
|
25
|
+
### Suggesting Enhancements
|
26
|
+
|
27
|
+
This section guides you through submitting an enhancement suggestion for RubyMCP.
|
28
|
+
|
29
|
+
Enhancement suggestions are tracked as GitHub issues. When creating an enhancement suggestion:
|
30
|
+
|
31
|
+
* **Use a clear and descriptive title**
|
32
|
+
* **Provide a detailed description of the suggested enhancement**
|
33
|
+
* **Explain why this enhancement would be useful**
|
34
|
+
* **Specify which version you're using**
|
35
|
+
* **Specify the name and version of the OS you're using**
|
36
|
+
|
37
|
+
### Pull Requests
|
38
|
+
|
39
|
+
* Fill in the required template
|
40
|
+
* Follow the Ruby style guide
|
41
|
+
* Include tests for new features
|
42
|
+
* Document new code based on the rest of the codebase
|
43
|
+
* End all files with a newline
|
44
|
+
|
45
|
+
## Development Process
|
46
|
+
|
47
|
+
### Setting Up Development Environment
|
48
|
+
|
49
|
+
```bash
|
50
|
+
# Fork and clone the repository
|
51
|
+
git clone https://github.com/yourusername/ruby_mcp.git
|
52
|
+
cd ruby_mcp
|
53
|
+
|
54
|
+
# Install dependencies
|
55
|
+
bundle install
|
56
|
+
|
57
|
+
# Run tests
|
58
|
+
bundle exec rspec
|
59
|
+
|
60
|
+
|
61
|
+
### Testing
|
62
|
+
|
63
|
+
Write tests for all new features
|
64
|
+
Ensure all tests pass before submitting a pull request
|
65
|
+
Run the full test suite locally before submission
|
66
|
+
|
67
|
+
```bash
|
68
|
+
bundle exec rspec
|
69
|
+
```
|
70
|
+
|
71
|
+
### Style Guidelines
|
72
|
+
|
73
|
+
Code should follow the Ruby style guide
|
74
|
+
Run RuboCop to check your code style
|
75
|
+
|
76
|
+
```bash
|
77
|
+
bundle exec rubocop
|
78
|
+
```
|
79
|
+
|
80
|
+
### Release Process
|
81
|
+
|
82
|
+
RubyMCP follows Semantic Versioning.
|
83
|
+
|
84
|
+
- MAJOR version for incompatible API changes
|
85
|
+
- MINOR version for backward-compatible functionality additions
|
86
|
+
- PATCH version for backward-compatible bug fixes
|
87
|
+
|
88
|
+
### First-time Contributors
|
89
|
+
If you're new to the project, look for issues labeled with good first issue which are ideal starting points for newcomers.
|
90
|
+
|
91
|
+
### License
|
92
|
+
By contributing to RubyMCP, you agree that your contributions will be licensed under the project's MIT License.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Nagendra Dhanakeerthi
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,491 @@
|
|
1
|
+
<div align="center">
|
2
|
+
|
3
|
+
# RubyMCP
|
4
|
+
|
5
|
+
[](https://github.com/nagstler/ruby_mcp/actions/workflows/build.yml)
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
7
|
+
[](https://github.com/nagstler/ruby_mcp/actions/workflows/rubocop.yml)
|
8
|
+
[](https://github.com/nagstler/ruby_mcp/actions/workflows/test.yml)
|
9
|
+
[](https://codecov.io/github/nagstler/ruby_mcp)
|
10
|
+
|
11
|
+
<strong>The Ruby way to build MCP servers and clients.</strong>
|
12
|
+
</div>
|
13
|
+
|
14
|
+
## Introduction
|
15
|
+
|
16
|
+
The [Model Context Protocol (MCP)](https://modelcontextprotocol.io) provides a standardized way for applications to interact with various language model providers (like OpenAI, Anthropic, etc.) through a consistent interface.
|
17
|
+
|
18
|
+

|
19
|
+
|
20
|
+
## Table of Contents
|
21
|
+
|
22
|
+
- [Introduction](#introduction)
|
23
|
+
- [Why RubyMCP?](#why-rubymcp)
|
24
|
+
- [Installation](#installation)
|
25
|
+
- [Quick Start](#quick-start)
|
26
|
+
- [Interactive Demo](#interactive-demo)
|
27
|
+
- [Configuration Options](#configuration-options)
|
28
|
+
- [Server Endpoints](#server-endpoints)
|
29
|
+
- [Detailed Usage](#detailed-usage)
|
30
|
+
- [Creating a Context](#creating-a-context)
|
31
|
+
- [Adding a Message](#adding-a-message)
|
32
|
+
- [Generating a Response](#generating-a-response)
|
33
|
+
- [Streaming a Response](#streaming-a-response)
|
34
|
+
- [Uploading Content](#uploading-content)
|
35
|
+
- [Using Tool Calls](#using-tool-calls)
|
36
|
+
- [Rails Integration](#rails-integration)
|
37
|
+
- [Custom Storage Backend](#custom-storage-backend)
|
38
|
+
- [Authentication](#authentication)
|
39
|
+
- [Development](#development)
|
40
|
+
- [Roadmap](#roadmap)
|
41
|
+
- [Contributing](#contributing)
|
42
|
+
- [License](#license)
|
43
|
+
|
44
|
+
## Why RubyMCP?
|
45
|
+
|
46
|
+
RubyMCP provides a Ruby implementation of the Model Context Protocol:
|
47
|
+
|
48
|
+
- Standard API for multiple LLM providers
|
49
|
+
- Context management for conversations
|
50
|
+
- Support for streaming responses
|
51
|
+
- File handling capabilities
|
52
|
+
- Tool calling support
|
53
|
+
- Authentication
|
54
|
+
- Schema validation using dry-schema
|
55
|
+
|
56
|
+
The library is designed to be straightforward to use while maintaining full compatibility with the MCP specification.
|
57
|
+
|
58
|
+
## Installation
|
59
|
+
|
60
|
+
Add this line to your application's Gemfile:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
gem 'mcp_on_ruby'
|
64
|
+
```
|
65
|
+
|
66
|
+
And then execute:
|
67
|
+
|
68
|
+
```
|
69
|
+
$ bundle install
|
70
|
+
```
|
71
|
+
|
72
|
+
Or install it yourself as:
|
73
|
+
|
74
|
+
```
|
75
|
+
$ gem install mcp_on_ruby
|
76
|
+
```
|
77
|
+
|
78
|
+
## Quick Start
|
79
|
+
|
80
|
+
Here's how to get a basic MCP server running:
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
require 'ruby_mcp'
|
84
|
+
|
85
|
+
# Configure RubyMCP
|
86
|
+
RubyMCP.configure do |config|
|
87
|
+
config.providers = {
|
88
|
+
openai: { api_key: ENV['OPENAI_API_KEY'] },
|
89
|
+
anthropic: { api_key: ENV['ANTHROPIC_API_KEY'] }
|
90
|
+
}
|
91
|
+
end
|
92
|
+
|
93
|
+
# Start the MCP server
|
94
|
+
server = RubyMCP::Server::Controller.new
|
95
|
+
server.start # This will block the current thread
|
96
|
+
```
|
97
|
+
|
98
|
+
### Interactive Demo
|
99
|
+
|
100
|
+
The repository includes an interactive demo that walks through all the key MCP concepts:
|
101
|
+
|
102
|
+
First, start the example server:
|
103
|
+
|
104
|
+
```bash
|
105
|
+
cd examples/simple_server
|
106
|
+
ruby server.rb
|
107
|
+
```
|
108
|
+
|
109
|
+
Then, in a separate terminal, run the client:
|
110
|
+
|
111
|
+
```bash
|
112
|
+
cd examples/simple_server
|
113
|
+
ruby client.rb
|
114
|
+
```
|
115
|
+
This demo provides a guided tour of the MCP functionality, showing each step of creating contexts, adding messages, and generating responses with detailed explanations.
|
116
|
+
|
117
|
+
## Configuration Options
|
118
|
+
|
119
|
+
RubyMCP offers several configuration options:
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
RubyMCP.configure do |config|
|
123
|
+
# LLM Provider configurations
|
124
|
+
config.providers = {
|
125
|
+
openai: {
|
126
|
+
api_key: ENV['OPENAI_API_KEY'],
|
127
|
+
api_base: 'https://api.openai.com/v1' # Optional
|
128
|
+
},
|
129
|
+
anthropic: {
|
130
|
+
api_key: ENV['ANTHROPIC_API_KEY']
|
131
|
+
}
|
132
|
+
}
|
133
|
+
|
134
|
+
# Storage backend (:memory, :redis, :active_record, or custom)
|
135
|
+
config.storage = :memory
|
136
|
+
|
137
|
+
# Server settings
|
138
|
+
config.server_port = 3000
|
139
|
+
config.server_host = "0.0.0.0"
|
140
|
+
|
141
|
+
# Authentication settings
|
142
|
+
config.auth_required = false
|
143
|
+
config.jwt_secret = ENV['JWT_SECRET']
|
144
|
+
config.token_expiry = 3600 # 1 hour
|
145
|
+
|
146
|
+
# Limits
|
147
|
+
config.max_contexts = 1000
|
148
|
+
end
|
149
|
+
```
|
150
|
+
|
151
|
+
## Server Endpoints
|
152
|
+
|
153
|
+
The MCP server provides the following endpoints:
|
154
|
+
|
155
|
+
### Engines
|
156
|
+
- `GET /engines` - List available language models
|
157
|
+
|
158
|
+
### Contexts
|
159
|
+
- `POST /contexts` - Create a new conversation context
|
160
|
+
- `GET /contexts` - List existing contexts
|
161
|
+
- `GET /contexts/:id` - Get details of a specific context
|
162
|
+
- `DELETE /contexts/:id` - Delete a context
|
163
|
+
|
164
|
+
### Messages
|
165
|
+
- `POST /messages` - Add a message to a context
|
166
|
+
|
167
|
+
### Generation
|
168
|
+
- `POST /generate` - Generate a response from a language model
|
169
|
+
- `POST /generate/stream` - Stream a response with incremental updates
|
170
|
+
|
171
|
+
### Content
|
172
|
+
- `POST /content` - Upload content (files)
|
173
|
+
- `GET /content/:context_id/:id` - Retrieve uploaded content
|
174
|
+
|
175
|
+
## Detailed Usage
|
176
|
+
|
177
|
+
### Creating a Context
|
178
|
+
|
179
|
+
```ruby
|
180
|
+
# Using the HTTP API
|
181
|
+
response = Faraday.post(
|
182
|
+
"http://localhost:3000/contexts",
|
183
|
+
{
|
184
|
+
messages: [
|
185
|
+
{
|
186
|
+
role: "system",
|
187
|
+
content: "You are a helpful assistant."
|
188
|
+
}
|
189
|
+
],
|
190
|
+
metadata: {
|
191
|
+
user_id: "user_123",
|
192
|
+
conversation_name: "Technical Support"
|
193
|
+
}
|
194
|
+
}.to_json,
|
195
|
+
"Content-Type" => "application/json"
|
196
|
+
)
|
197
|
+
|
198
|
+
context_id = JSON.parse(response.body)["id"]
|
199
|
+
```
|
200
|
+
|
201
|
+
### Adding a Message
|
202
|
+
|
203
|
+
```ruby
|
204
|
+
Faraday.post(
|
205
|
+
"http://localhost:3000/messages",
|
206
|
+
{
|
207
|
+
context_id: context_id,
|
208
|
+
role: "user",
|
209
|
+
content: "What is the capital of France?"
|
210
|
+
}.to_json,
|
211
|
+
"Content-Type" => "application/json"
|
212
|
+
)
|
213
|
+
```
|
214
|
+
|
215
|
+
### Generating a Response
|
216
|
+
|
217
|
+
```ruby
|
218
|
+
response = Faraday.post(
|
219
|
+
"http://localhost:3000/generate",
|
220
|
+
{
|
221
|
+
context_id: context_id,
|
222
|
+
engine_id: "anthropic/claude-3-sonnet-20240229",
|
223
|
+
max_tokens: 1000,
|
224
|
+
temperature: 0.7
|
225
|
+
}.to_json,
|
226
|
+
"Content-Type" => "application/json"
|
227
|
+
)
|
228
|
+
|
229
|
+
assistant_response = JSON.parse(response.body)["content"]
|
230
|
+
```
|
231
|
+
|
232
|
+
### Streaming a Response
|
233
|
+
|
234
|
+
```ruby
|
235
|
+
conn = Faraday.new do |f|
|
236
|
+
f.request :json
|
237
|
+
f.response :json
|
238
|
+
f.adapter :net_http
|
239
|
+
end
|
240
|
+
|
241
|
+
conn.post("http://localhost:3000/generate/stream") do |req|
|
242
|
+
req.headers["Content-Type"] = "application/json"
|
243
|
+
req.body = {
|
244
|
+
context_id: context_id,
|
245
|
+
engine_id: "openai/gpt-4",
|
246
|
+
temperature: 0.7
|
247
|
+
}.to_json
|
248
|
+
|
249
|
+
req.options.on_data = Proc.new do |chunk, size, total|
|
250
|
+
event_data = chunk.split("data: ").last.strip
|
251
|
+
next if event_data.empty? || event_data == "[DONE]"
|
252
|
+
|
253
|
+
event = JSON.parse(event_data)
|
254
|
+
if event["event"] == "generation.content" && event["content"]
|
255
|
+
print event["content"]
|
256
|
+
end
|
257
|
+
end
|
258
|
+
end
|
259
|
+
```
|
260
|
+
|
261
|
+
### Uploading Content
|
262
|
+
|
263
|
+
```ruby
|
264
|
+
file_data = Base64.strict_encode64(File.read("example.pdf"))
|
265
|
+
|
266
|
+
Faraday.post(
|
267
|
+
"http://localhost:3000/content",
|
268
|
+
{
|
269
|
+
context_id: context_id,
|
270
|
+
type: "file",
|
271
|
+
filename: "example.pdf",
|
272
|
+
content_type: "application/pdf",
|
273
|
+
file_data: file_data
|
274
|
+
}.to_json,
|
275
|
+
"Content-Type" => "application/json"
|
276
|
+
)
|
277
|
+
```
|
278
|
+
|
279
|
+
### Using Tool Calls
|
280
|
+
|
281
|
+
```ruby
|
282
|
+
tools = [
|
283
|
+
{
|
284
|
+
type: "function",
|
285
|
+
function: {
|
286
|
+
name: "get_weather",
|
287
|
+
description: "Get the current weather for a location",
|
288
|
+
parameters: {
|
289
|
+
type: "object",
|
290
|
+
properties: {
|
291
|
+
location: {
|
292
|
+
type: "string",
|
293
|
+
description: "City and state, e.g., San Francisco, CA"
|
294
|
+
}
|
295
|
+
},
|
296
|
+
required: ["location"]
|
297
|
+
}
|
298
|
+
}
|
299
|
+
}
|
300
|
+
]
|
301
|
+
|
302
|
+
response = Faraday.post(
|
303
|
+
"http://localhost:3000/generate",
|
304
|
+
{
|
305
|
+
context_id: context_id,
|
306
|
+
engine_id: "openai/gpt-4",
|
307
|
+
tools: tools
|
308
|
+
}.to_json,
|
309
|
+
"Content-Type" => "application/json"
|
310
|
+
)
|
311
|
+
|
312
|
+
if response.body["tool_calls"]
|
313
|
+
# Handle tool calls
|
314
|
+
tool_calls = response.body["tool_calls"]
|
315
|
+
# Process tool calls and add tool response message
|
316
|
+
end
|
317
|
+
```
|
318
|
+
|
319
|
+
## Rails Integration
|
320
|
+
|
321
|
+
For Rails applications, create an initializer at `config/initializers/ruby_mcp.rb`:
|
322
|
+
|
323
|
+
```ruby
|
324
|
+
RubyMCP.configure do |config|
|
325
|
+
config.providers = {
|
326
|
+
openai: { api_key: ENV['OPENAI_API_KEY'] },
|
327
|
+
anthropic: { api_key: ENV['ANTHROPIC_API_KEY'] }
|
328
|
+
}
|
329
|
+
|
330
|
+
# Use memory storage in development, consider persistent storage in production
|
331
|
+
if Rails.env.development? || Rails.env.test?
|
332
|
+
config.storage = :memory
|
333
|
+
else
|
334
|
+
config.storage = :memory # Replace with persistent option when implemented
|
335
|
+
end
|
336
|
+
|
337
|
+
# Enable authentication in production
|
338
|
+
if Rails.env.production?
|
339
|
+
config.auth_required = true
|
340
|
+
config.jwt_secret = ENV["JWT_SECRET"]
|
341
|
+
end
|
342
|
+
end
|
343
|
+
```
|
344
|
+
|
345
|
+
And mount the server in your `config/routes.rb` file:
|
346
|
+
|
347
|
+
```ruby
|
348
|
+
Rails.application.routes.draw do
|
349
|
+
# Mount RubyMCP at /api/mcp
|
350
|
+
mount_mcp_at = "/api/mcp"
|
351
|
+
|
352
|
+
Rails.application.config.middleware.use Rack::Config do |env|
|
353
|
+
env["SCRIPT_NAME"] = mount_mcp_at if env["PATH_INFO"].start_with?(mount_mcp_at)
|
354
|
+
end
|
355
|
+
|
356
|
+
mount RubyMCP::Server::App.new.rack_app, at: mount_mcp_at
|
357
|
+
|
358
|
+
# Rest of your routes
|
359
|
+
# ...
|
360
|
+
end
|
361
|
+
```
|
362
|
+
|
363
|
+
## Custom Storage Backend
|
364
|
+
|
365
|
+
You can implement custom storage backends by extending the base storage class:
|
366
|
+
|
367
|
+
```ruby
|
368
|
+
class RedisStorage < RubyMCP::Storage::Base
|
369
|
+
def initialize(options = {})
|
370
|
+
super
|
371
|
+
@redis = Redis.new(options)
|
372
|
+
end
|
373
|
+
|
374
|
+
def create_context(context)
|
375
|
+
@redis.set("context:#{context.id}", JSON.dump(context.to_h))
|
376
|
+
context
|
377
|
+
end
|
378
|
+
|
379
|
+
def get_context(context_id)
|
380
|
+
data = @redis.get("context:#{context_id}")
|
381
|
+
raise RubyMCP::Errors::ContextError, "Context not found: #{context_id}" unless data
|
382
|
+
|
383
|
+
hash = JSON.parse(data, symbolize_names: true)
|
384
|
+
|
385
|
+
# Create message objects
|
386
|
+
messages = hash[:messages].map do |msg|
|
387
|
+
RubyMCP::Models::Message.new(
|
388
|
+
role: msg[:role],
|
389
|
+
content: msg[:content],
|
390
|
+
id: msg[:id],
|
391
|
+
metadata: msg[:metadata]
|
392
|
+
)
|
393
|
+
end
|
394
|
+
|
395
|
+
# Create the context
|
396
|
+
RubyMCP::Models::Context.new(
|
397
|
+
id: hash[:id],
|
398
|
+
messages: messages,
|
399
|
+
metadata: hash[:metadata]
|
400
|
+
)
|
401
|
+
end
|
402
|
+
|
403
|
+
# Implement other required methods...
|
404
|
+
end
|
405
|
+
|
406
|
+
# Configure RubyMCP to use your custom storage
|
407
|
+
RubyMCP.configure do |config|
|
408
|
+
config.storage = RedisStorage.new(url: ENV["REDIS_URL"])
|
409
|
+
end
|
410
|
+
```
|
411
|
+
|
412
|
+
## Authentication
|
413
|
+
|
414
|
+
To enable JWT authentication:
|
415
|
+
|
416
|
+
```ruby
|
417
|
+
RubyMCP.configure do |config|
|
418
|
+
config.auth_required = true
|
419
|
+
config.jwt_secret = ENV['JWT_SECRET']
|
420
|
+
config.token_expiry = 3600 # 1 hour
|
421
|
+
end
|
422
|
+
```
|
423
|
+
|
424
|
+
Then, create and use JWT tokens:
|
425
|
+
|
426
|
+
```ruby
|
427
|
+
# Generate a token
|
428
|
+
require 'jwt'
|
429
|
+
|
430
|
+
payload = {
|
431
|
+
sub: "user_123",
|
432
|
+
exp: Time.now.to_i + 3600
|
433
|
+
}
|
434
|
+
|
435
|
+
token = JWT.encode(payload, ENV['JWT_SECRET'], 'HS256')
|
436
|
+
|
437
|
+
# Use the token in requests
|
438
|
+
conn = Faraday.new do |f|
|
439
|
+
f.request :json
|
440
|
+
f.response :json
|
441
|
+
f.adapter :net_http
|
442
|
+
end
|
443
|
+
|
444
|
+
conn.get("http://localhost:3000/contexts") do |req|
|
445
|
+
req.headers["Authorization"] = "Bearer #{token}"
|
446
|
+
end
|
447
|
+
```
|
448
|
+
|
449
|
+
## Development
|
450
|
+
|
451
|
+
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.
|
452
|
+
|
453
|
+
### Running Tests
|
454
|
+
|
455
|
+
```
|
456
|
+
bundle exec rspec
|
457
|
+
```
|
458
|
+
|
459
|
+
### Local Development Server
|
460
|
+
|
461
|
+
```
|
462
|
+
bundle exec ruby examples/simple_server/server.rb
|
463
|
+
```
|
464
|
+
|
465
|
+
## Roadmap
|
466
|
+
|
467
|
+
While RubyMCP is functional for basic use cases, there are several areas planned for improvement:
|
468
|
+
|
469
|
+
- [ ] Persistent storage backends (Redis, ActiveRecord)
|
470
|
+
- [ ] Complete test coverage, including integration tests
|
471
|
+
- [ ] Improved error handling and recovery strategies
|
472
|
+
- [ ] Rate limiting for provider APIs
|
473
|
+
- [ ] Proper tokenization for context window management
|
474
|
+
- [ ] More robust streaming implementation
|
475
|
+
- [ ] Additional provider integrations
|
476
|
+
|
477
|
+
:heart: Contributions in any of these areas are welcome!
|
478
|
+
|
479
|
+
## Contributing
|
480
|
+
|
481
|
+
1. Fork the repository
|
482
|
+
2. Create your feature branch (`git checkout -b feature/my-new-feature`)
|
483
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
484
|
+
4. Push to the branch (`git push origin feature/my-new-feature`)
|
485
|
+
5. Create a new Pull Request
|
486
|
+
|
487
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nagstler/mcp_on_ruby.
|
488
|
+
|
489
|
+
## License
|
490
|
+
|
491
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|