stateless-mcp 0.0.1
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 +18 -0
- data/LICENSE-APACHE +203 -0
- data/LICENSE-MIT +21 -0
- data/README.md +13 -0
- data/lib/stateless-mcp.rb +1 -0
- data/lib/stateless_mcp/cursor.rb +22 -0
- data/lib/stateless_mcp/header_check.rb +53 -0
- data/lib/stateless_mcp/middleware.rb +54 -0
- data/lib/stateless_mcp/pagination.rb +39 -0
- data/lib/stateless_mcp/refusal.rb +18 -0
- data/lib/stateless_mcp/request_body.rb +29 -0
- data/lib/stateless_mcp/result.rb +32 -0
- data/lib/stateless_mcp/rpc.rb +24 -0
- data/lib/stateless_mcp/server.rb +128 -0
- data/lib/stateless_mcp/tool_declaration.rb +70 -0
- data/lib/stateless_mcp/version.rb +4 -0
- data/lib/stateless_mcp.rb +41 -0
- metadata +127 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: acccfae88cee7f634e762e143c5599f2b76ef514f1534555e8407a0f1cb297e0
|
|
4
|
+
data.tar.gz: f118030ce51d056a96520e008dec2fb21508cb4fc73cf8f258942727efdb8537
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: a174f3a50fee8ff7fa31f495642685716a1586c7b1cdb97592e0680cf2794a640a7013261c6ffbf7ce770c463f78130a578d57798ecf0a642cf719bc3866d5e1
|
|
7
|
+
data.tar.gz: 3f471987cc08462b81ee9865ceb28ac673d42e2f71f48fae48a9d896bc86c000bea2a2b5062c4808902bd7796c753486a3c2576e475d30e8bc4e7b986fd31f66
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
Changes to the `stateless-mcp` gem. The Rails integration has [its own changelog](../stateless-mcp-rails/CHANGELOG.md). The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and versions follow [Semantic Versioning](https://semver.org/).
|
|
4
|
+
|
|
5
|
+
## [Unreleased]
|
|
6
|
+
|
|
7
|
+
## [0.0.1] - 2026-10-19
|
|
8
|
+
|
|
9
|
+
First release: a Rack endpoint for the stateless MCP protocol, 2026-07-28.
|
|
10
|
+
|
|
11
|
+
- `StatelessMcp::Server`: one endpoint's settings, validated, with a default for each.
|
|
12
|
+
- `StatelessMcp::Middleware`: reads the JSON-RPC body within the server's limit, checks `Origin`, and refuses requests whose headers disagree with the body.
|
|
13
|
+
- JSON-RPC errors with the HTTP status the spec gives each.
|
|
14
|
+
- `StatelessMcp::ToolDeclaration`, `StatelessMcp::Result` and `StatelessMcp::Pagination`: tool listings, results, and cursor paging of lists and long text.
|
|
15
|
+
- Guides and examples for plain Rack and Sinatra.
|
|
16
|
+
|
|
17
|
+
[Unreleased]: https://github.com/Jamedjo/stateless-mcp/compare/v0.0.1...HEAD
|
|
18
|
+
[0.0.1]: https://github.com/Jamedjo/stateless-mcp/tree/v0.0.1/stateless-mcp
|
data/LICENSE-APACHE
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
203
|
+
|
data/LICENSE-MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 James Edwards-Jones
|
|
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,13 @@
|
|
|
1
|
+
# stateless-mcp
|
|
2
|
+
|
|
3
|
+
Rack middleware and building blocks for serving [Model Context Protocol](https://modelcontextprotocol.io/specification/2026-07-28) tools without sessions, from any Rack app, Sinatra included.
|
|
4
|
+
|
|
5
|
+
> **Status:** early. Releases before 1.0 may change the API.
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
gem "stateless-mcp"
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
The documentation is in the [main README](https://github.com/Jamedjo/stateless-mcp#readme), with guides for [plain Rack](https://github.com/Jamedjo/stateless-mcp/blob/main/docs/rack.md) and [Sinatra](https://github.com/Jamedjo/stateless-mcp/blob/main/docs/sinatra.md). Rails apps can use [`stateless-mcp-rails`](https://github.com/Jamedjo/stateless-mcp/tree/main/stateless-mcp-rails) instead.
|
|
12
|
+
|
|
13
|
+
Licensed under either of Apache-2.0 or MIT, at your option.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require "stateless_mcp"
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
module StatelessMcp
|
|
2
|
+
# Cursors are opaque to the client, so a model carries next_cursor forward
|
|
3
|
+
# rather than inventing positions of its own. Inside, a cursor is a JSON
|
|
4
|
+
# array of values in URL-safe base64.
|
|
5
|
+
module Cursor
|
|
6
|
+
# Raised for a cursor this library didn't issue.
|
|
7
|
+
Invalid = Class.new(StandardError)
|
|
8
|
+
|
|
9
|
+
def self.encode(*values)
|
|
10
|
+
Base64.urlsafe_encode64(JSON.generate(values), padding: false)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# The values encoded, as an array of exactly count of them when count is given.
|
|
14
|
+
def self.decode(cursor, count: nil)
|
|
15
|
+
values = JSON.parse(Base64.urlsafe_decode64(cursor.to_s))
|
|
16
|
+
raise Invalid unless values.is_a?(Array) && (count.nil? || values.size == count)
|
|
17
|
+
values
|
|
18
|
+
rescue ArgumentError, EncodingError, JSON::ParserError
|
|
19
|
+
raise Invalid
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
module StatelessMcp
|
|
2
|
+
# 2026-07-28 clients mirror the method and its target into Mcp-Method and
|
|
3
|
+
# Mcp-Name for intermediaries to route on, and the spec requires refusing any
|
|
4
|
+
# request whose headers disagree with its body: otherwise a proxy could log
|
|
5
|
+
# one method while the server runs another.
|
|
6
|
+
module HeaderCheck
|
|
7
|
+
# Raises a Refusal for the first disagreement. A base64-wrapped Mcp-Name is
|
|
8
|
+
# decoded in env, so routes match the name itself.
|
|
9
|
+
def self.check!(env, body)
|
|
10
|
+
check_version(env["HTTP_MCP_PROTOCOL_VERSION"])
|
|
11
|
+
must_match("Mcp-Method", env["HTTP_MCP_METHOD"], body["method"])
|
|
12
|
+
env["HTTP_MCP_NAME"] = decode(env["HTTP_MCP_NAME"]) if env["HTTP_MCP_NAME"]
|
|
13
|
+
target = StatelessMcp.target_name(body)
|
|
14
|
+
must_match("Mcp-Name", env["HTTP_MCP_NAME"], target) if target
|
|
15
|
+
check_envelope(env, body) unless MCP::Methods.notification?(body["method"])
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Header values outside plain ASCII arrive base64 encoded inside a
|
|
19
|
+
# =?base64?…?= wrapper.
|
|
20
|
+
def self.decode(value)
|
|
21
|
+
encoded = value.to_s[/\A=\?base64\?(.*)\?=\z/m, 1]
|
|
22
|
+
encoded ? Base64.strict_decode64(encoded).force_encoding(Encoding::UTF_8) : value
|
|
23
|
+
rescue ArgumentError
|
|
24
|
+
value
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# A missing header is a mismatch; a version this server doesn't speak is
|
|
28
|
+
# answered with the versions it does.
|
|
29
|
+
def self.check_version(version)
|
|
30
|
+
raise header_mismatch("MCP-Protocol-Version header is required") if version.to_s.empty?
|
|
31
|
+
return if MCP::Configuration.modern_protocol_version?(version)
|
|
32
|
+
raise Refusal.new(MCP::ErrorCodes::UNSUPPORTED_PROTOCOL_VERSION, "Unsupported protocol version", data: { supported: MCP::Configuration::SUPPORTED_MODERN_PROTOCOL_VERSIONS, requested: version })
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.check_envelope(env, body)
|
|
36
|
+
must_match("MCP-Protocol-Version", env["HTTP_MCP_PROTOCOL_VERSION"], body.dig("params", "_meta", MCP::RequestEnvelope::PROTOCOL_VERSION_META_KEY)) if MCP::RequestEnvelope.modern?(body["params"])
|
|
37
|
+
MCP::RequestEnvelope.parse!(body["params"])
|
|
38
|
+
rescue MCP::Server::RequestHandlerError => error
|
|
39
|
+
raise Refusal.new(error.error_code, error.message, data: error.error_data)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def self.must_match(header, sent, expected)
|
|
43
|
+
return if sent == expected
|
|
44
|
+
raise header_mismatch(sent.to_s.empty? ? "#{header} header is required" : "#{header} header value '#{sent}' does not match body value '#{expected}'")
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.header_mismatch(detail)
|
|
48
|
+
Refusal.new(MCP::ErrorCodes::HEADER_MISMATCH, "Header mismatch: #{detail}")
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
private_class_method :check_version, :check_envelope, :must_match, :header_mismatch
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
module StatelessMcp
|
|
2
|
+
# Rack middleware for one server's endpoint. A POST to the server's path has
|
|
3
|
+
# its body read within the server's limit and its headers checked against
|
|
4
|
+
# the body; the app then finds the server in env[Server::ENV_KEY] and the
|
|
5
|
+
# parsed body in env[REQUEST_ENV_KEY]. Requests to other paths pass through
|
|
6
|
+
# untouched.
|
|
7
|
+
#
|
|
8
|
+
# use StatelessMcp::Middleware, server: StatelessMcp::Server.new(path: "/mcp")
|
|
9
|
+
#
|
|
10
|
+
# A request whose Origin header names another site is refused with a 403
|
|
11
|
+
# before anything else, unless the server allows that origin or has
|
|
12
|
+
# check_origin off. Authentication and rate limits are the app's, so
|
|
13
|
+
# middleware doing them goes before this one: a request it refuses is then
|
|
14
|
+
# never read.
|
|
15
|
+
class Middleware
|
|
16
|
+
def initialize(app, server:)
|
|
17
|
+
raise ArgumentError, "server: must be a StatelessMcp::Server; got #{server.inspect}" unless server.is_a?(Server)
|
|
18
|
+
@app = app
|
|
19
|
+
@server = server
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def call(env)
|
|
23
|
+
return @app.call(env) unless @server.endpoint?(env)
|
|
24
|
+
env[Server::ENV_KEY] = @server
|
|
25
|
+
return forbidden_origin unless @server.origin_allowed?(env)
|
|
26
|
+
return method_not_allowed if ["GET", "DELETE"].include?(env["REQUEST_METHOD"])
|
|
27
|
+
return @app.call(env) unless env["REQUEST_METHOD"] == "POST"
|
|
28
|
+
body = env[REQUEST_ENV_KEY] = RequestBody.read(env, max_bytes: @server.max_body_bytes)
|
|
29
|
+
check(env, body)
|
|
30
|
+
@app.call(env)
|
|
31
|
+
rescue Refusal => refusal
|
|
32
|
+
refusal.to_rack(env[REQUEST_ENV_KEY])
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
# Every request is held to 2026-07-28. Serving earlier protocol revisions
|
|
38
|
+
# means deciding here which rules a request is held to.
|
|
39
|
+
def check(env, body)
|
|
40
|
+
HeaderCheck.check!(env, body)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# The spec allows a JSON-RPC error without an id in the body.
|
|
44
|
+
def forbidden_origin
|
|
45
|
+
[403, { "content-type" => "application/json" }, [JSON.generate(Rpc.error(nil, Rpc::ErrorCode::INVALID_REQUEST, "Origin not allowed"))]]
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Clients on earlier revisions open a stream with GET and end a session
|
|
49
|
+
# with DELETE; the spec has a stateless server answer both with 405.
|
|
50
|
+
def method_not_allowed
|
|
51
|
+
[405, { "allow" => "POST" }, []]
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
module StatelessMcp
|
|
2
|
+
# Paging tool results by cursor. A tool declares `paginated` among its
|
|
3
|
+
# arguments, which adds limit and cursor, and pages with these. A list or
|
|
4
|
+
# text is paged by position, so it suits data that doesn't change between
|
|
5
|
+
# calls; records that do are better paged by key, as the Rails ActiveRecord
|
|
6
|
+
# add-on does.
|
|
7
|
+
module Pagination
|
|
8
|
+
# The page size a call asked for, held to the server's limits.
|
|
9
|
+
def self.limit(requested, server)
|
|
10
|
+
(requested.nil? || requested.to_s.empty? ? server.page_size : Integer(requested)).clamp(1, server.max_page_size)
|
|
11
|
+
rescue ArgumentError, TypeError
|
|
12
|
+
server.page_size
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# One page of items, and the cursor for the next while more remain.
|
|
16
|
+
def self.list(items, cursor:, limit:)
|
|
17
|
+
start = offset(cursor)
|
|
18
|
+
rows = items[start, limit + 1] || []
|
|
19
|
+
[rows.first(limit), (Cursor.encode(start + limit) if rows.size > limit)]
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# A long text in parts of part_size characters, and the cursor for the
|
|
23
|
+
# next part while more remains. The block reads length characters of the
|
|
24
|
+
# text from start, so a text kept elsewhere is never read whole.
|
|
25
|
+
def self.text(cursor, part_size)
|
|
26
|
+
start = offset(cursor)
|
|
27
|
+
slice = yield(start, part_size + 1).to_s
|
|
28
|
+
[slice[0, part_size], (Cursor.encode(start + part_size) if slice.length > part_size)]
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Where a list or text continues from: the start, without a cursor.
|
|
32
|
+
def self.offset(cursor)
|
|
33
|
+
return 0 if cursor.nil? || cursor.to_s.empty?
|
|
34
|
+
position = Cursor.decode(cursor, count: 1).first
|
|
35
|
+
raise Cursor::Invalid unless position.is_a?(Integer) && !position.negative?
|
|
36
|
+
position
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module StatelessMcp
|
|
2
|
+
# A request turned away before it reaches the app, answered as a JSON-RPC
|
|
3
|
+
# error.
|
|
4
|
+
class Refusal < StandardError
|
|
5
|
+
attr_reader :code, :data
|
|
6
|
+
|
|
7
|
+
def initialize(code, message, data: nil)
|
|
8
|
+
super(message)
|
|
9
|
+
@code = code
|
|
10
|
+
@data = data
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def to_rack(body)
|
|
14
|
+
id = body["id"] if body.is_a?(Hash)
|
|
15
|
+
[Rpc.status_for(code), { "content-type" => "application/json" }, [JSON.generate(Rpc.error(id, code, message, data: data))]]
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
module StatelessMcp
|
|
2
|
+
# Reads and parses a request's JSON-RPC body, refusing it before reading
|
|
3
|
+
# past max_bytes. The body is put back as a fresh input, so the app can read
|
|
4
|
+
# it again as it would any other request.
|
|
5
|
+
module RequestBody
|
|
6
|
+
def self.read(env, max_bytes:)
|
|
7
|
+
raise too_large(max_bytes) if env["CONTENT_LENGTH"].to_i > max_bytes
|
|
8
|
+
raw = env["rack.input"]&.read(max_bytes + 1).to_s
|
|
9
|
+
raise too_large(max_bytes) if raw.bytesize > max_bytes
|
|
10
|
+
env["rack.input"] = StringIO.new(raw)
|
|
11
|
+
body = JSON.parse(raw)
|
|
12
|
+
raise Refusal.new(Rpc::ErrorCode::INVALID_REQUEST, "Body must be a single JSON-RPC request object") unless single_request?(body)
|
|
13
|
+
body
|
|
14
|
+
rescue JSON::ParserError
|
|
15
|
+
raise Refusal.new(Rpc::ErrorCode::PARSE_ERROR, "Parse error")
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Batches are refused: 2026-07-28 carries one message per POST.
|
|
19
|
+
def self.single_request?(body)
|
|
20
|
+
body.is_a?(Hash) && JsonRpcHandler.valid_version?(body["jsonrpc"]) && JsonRpcHandler.valid_method_name?(body["method"])
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.too_large(max_bytes)
|
|
24
|
+
Refusal.new(Rpc::ErrorCode::INVALID_REQUEST, "Request body is larger than #{max_bytes} bytes")
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private_class_method :single_request?, :too_large
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
module StatelessMcp
|
|
2
|
+
# The result objects a server answers with, before they are wrapped in a
|
|
3
|
+
# JSON-RPC response.
|
|
4
|
+
module Result
|
|
5
|
+
# Every 2026-07-28 result names its type. A cacheable one also carries the
|
|
6
|
+
# spec's cache hints, set so a client caches it only for this caller and
|
|
7
|
+
# checks again before reusing it.
|
|
8
|
+
def self.complete(result, cacheable: false)
|
|
9
|
+
result = { ttlMs: 0, cacheScope: "private" }.merge(result) if cacheable
|
|
10
|
+
result.merge(resultType: MCP::ResultType::COMPLETE)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# A tool's answer, as JSON in one text block, leaving out a hash's nil
|
|
14
|
+
# values. Clients hand the text to the model. structuredContent is for
|
|
15
|
+
# programs validating against an outputSchema, which these tools don't
|
|
16
|
+
# declare, so sending it as well would only double every result.
|
|
17
|
+
def self.tool(payload)
|
|
18
|
+
payload = payload.compact if payload.is_a?(Hash)
|
|
19
|
+
complete(MCP::Tool::Response.new([{ type: "text", text: payload.to_json }]).to_h)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# A tool call turned down with a message the model can read and act on.
|
|
23
|
+
def self.refusal(message)
|
|
24
|
+
complete(MCP::Tool::Response.new([{ type: "text", text: message }], error: true).to_h)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# The answer to server/discover.
|
|
28
|
+
def self.discover(server, capabilities:)
|
|
29
|
+
complete({ supportedVersions: MCP::Configuration::SUPPORTED_MODERN_PROTOCOL_VERSIONS, capabilities: capabilities, instructions: server.instructions, _meta: { MCP::RequestEnvelope::SERVER_INFO_META_KEY => server.info } }.compact, cacheable: true)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module StatelessMcp
|
|
2
|
+
# JSON-RPC envelopes, and the HTTP status each error travels with. The
|
|
3
|
+
# envelopes are the mcp gem's own.
|
|
4
|
+
module Rpc
|
|
5
|
+
ErrorCode = JsonRpcHandler::ErrorCode
|
|
6
|
+
BAD_REQUEST_CODES = MCP::Server::Transports::StreamableHTTPTransport::MODERN_BAD_REQUEST_CODES
|
|
7
|
+
|
|
8
|
+
def self.success(id, result)
|
|
9
|
+
JsonRpcHandler.success_response(id: id, result: result)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# A request whose id is missing or malformed is answered with a null id.
|
|
13
|
+
def self.error(id, code, message, data: nil)
|
|
14
|
+
JsonRpcHandler.error_response(id: id.nil? ? :unknown_id : id, id_validation_pattern: JsonRpcHandler::DEFAULT_ALLOWED_ID_CHARACTERS, error: { code: code, message: message, data: data })
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# A 404 carrying a JSON-RPC body tells a client it reached a current server
|
|
18
|
+
# that lacks the method, rather than a missing endpoint.
|
|
19
|
+
def self.status_for(code)
|
|
20
|
+
return 404 if code == ErrorCode::METHOD_NOT_FOUND
|
|
21
|
+
BAD_REQUEST_CODES.include?(code) ? 400 : 200
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
module StatelessMcp
|
|
2
|
+
# One MCP endpoint: where it is served, how it describes itself, and the
|
|
3
|
+
# limits it holds requests to. Every other part of the library is handed the
|
|
4
|
+
# server it serves, so an app can run several side by side:
|
|
5
|
+
#
|
|
6
|
+
# admin = StatelessMcp::Server.new(path: "/admin/mcp", name: "admin", max_body_bytes: 16_384)
|
|
7
|
+
#
|
|
8
|
+
# A server is frozen once built, so it can be shared between threads.
|
|
9
|
+
class Server
|
|
10
|
+
# Raised for a setting that can't be served, naming the setting.
|
|
11
|
+
Invalid = Class.new(ArgumentError)
|
|
12
|
+
|
|
13
|
+
# The server a request was routed to, which the middleware leaves in the Rack env.
|
|
14
|
+
ENV_KEY = "stateless_mcp.server"
|
|
15
|
+
|
|
16
|
+
# How a resource's tools are named from its actions: index on notes is list_notes.
|
|
17
|
+
DEFAULT_TOOL_VERBS = { index: "list", show: "get", create: "create", update: "update", destroy: "delete" }.freeze
|
|
18
|
+
|
|
19
|
+
DEFAULTS = {
|
|
20
|
+
path: "/mcp",
|
|
21
|
+
name: "mcp",
|
|
22
|
+
version: "1.0.0",
|
|
23
|
+
title: nil,
|
|
24
|
+
instructions: nil,
|
|
25
|
+
max_body_bytes: 1_048_576,
|
|
26
|
+
page_size: 20,
|
|
27
|
+
max_page_size: 100,
|
|
28
|
+
tool_verbs: DEFAULT_TOOL_VERBS,
|
|
29
|
+
# The MCP spec has servers refuse a request whose Origin header names
|
|
30
|
+
# another site, so a page in a browser can't drive the tools with the
|
|
31
|
+
# visitor's credentials. Requests without an Origin, from non-browser
|
|
32
|
+
# clients, are unaffected.
|
|
33
|
+
check_origin: true,
|
|
34
|
+
# Origins other than the server's own that may call it, such as
|
|
35
|
+
# "https://app.example".
|
|
36
|
+
allowed_origins: []
|
|
37
|
+
}.freeze
|
|
38
|
+
|
|
39
|
+
attr_reader(*DEFAULTS.keys)
|
|
40
|
+
|
|
41
|
+
# tool_verbs are merged over the defaults, so an app can rename one verb
|
|
42
|
+
# without restating the rest.
|
|
43
|
+
def initialize(**settings)
|
|
44
|
+
unknown = settings.keys - DEFAULTS.keys
|
|
45
|
+
raise Invalid, "Unknown server settings: #{unknown.join(", ")}" if unknown.any?
|
|
46
|
+
settings = DEFAULTS.merge(settings.compact)
|
|
47
|
+
@path = check_path(settings[:path])
|
|
48
|
+
@name = check_text(:name, settings[:name])
|
|
49
|
+
@version = check_text(:version, settings[:version])
|
|
50
|
+
@title = check_text(:title, settings[:title]) unless settings[:title].nil?
|
|
51
|
+
@instructions = check_text(:instructions, settings[:instructions]) unless settings[:instructions].nil?
|
|
52
|
+
@max_body_bytes = check_positive(:max_body_bytes, settings[:max_body_bytes])
|
|
53
|
+
@page_size = check_positive(:page_size, settings[:page_size])
|
|
54
|
+
@max_page_size = check_positive(:max_page_size, settings[:max_page_size])
|
|
55
|
+
raise Invalid, "page_size (#{page_size}) can't be larger than max_page_size (#{max_page_size})" if page_size > max_page_size
|
|
56
|
+
@tool_verbs = check_tool_verbs(settings[:tool_verbs])
|
|
57
|
+
@check_origin = check_boolean(:check_origin, settings[:check_origin])
|
|
58
|
+
@allowed_origins = check_origins(settings[:allowed_origins])
|
|
59
|
+
freeze
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Whether a request is addressed to this server's endpoint.
|
|
63
|
+
def endpoint?(env)
|
|
64
|
+
env["PATH_INFO"] == path
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Whether a request's Origin header, if it has one, may call this server:
|
|
68
|
+
# the server's own origin, or one of allowed_origins.
|
|
69
|
+
def origin_allowed?(env)
|
|
70
|
+
origin = env["HTTP_ORIGIN"]
|
|
71
|
+
return true if origin.nil? || !check_origin
|
|
72
|
+
origin = origin.downcase
|
|
73
|
+
origin == Rack::Request.new(env).base_url.downcase || allowed_origins.include?(origin)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# The serverInfo object the protocol describes a server with.
|
|
77
|
+
def info
|
|
78
|
+
{ name: name, title: title, version: version }.compact
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def inspect
|
|
82
|
+
"#<#{self.class.name} #{path} #{name}>"
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
private
|
|
86
|
+
|
|
87
|
+
def check_path(value)
|
|
88
|
+
return value.dup.freeze if value.is_a?(String) && value.match?(%r{\A/[^?#\s]*\z}) && (value == "/" || !value.end_with?("/"))
|
|
89
|
+
raise Invalid, "path must be an absolute path such as \"/mcp\", without a trailing slash, query or fragment; got #{value.inspect}"
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def check_text(setting, value)
|
|
93
|
+
return value.dup.freeze if value.is_a?(String) && !value.strip.empty?
|
|
94
|
+
raise Invalid, "#{setting} must be a non-empty string; got #{value.inspect}"
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def check_positive(setting, value)
|
|
98
|
+
return value if value.is_a?(Integer) && value.positive?
|
|
99
|
+
raise Invalid, "#{setting} must be a positive integer; got #{value.inspect}"
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def check_boolean(setting, value)
|
|
103
|
+
return value if value == true || value == false
|
|
104
|
+
raise Invalid, "#{setting} must be true or false; got #{value.inspect}"
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def check_origins(value)
|
|
108
|
+
raise Invalid, "allowed_origins must be an array of origins such as \"https://app.example\"; got #{value.inspect}" unless value.is_a?(Array)
|
|
109
|
+
value.map { |origin| check_origin_url(origin) }.freeze
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Only scheme, host and port: an Origin header carries nothing more.
|
|
113
|
+
def check_origin_url(value)
|
|
114
|
+
uri = URI.parse(value.to_s)
|
|
115
|
+
return value.downcase.freeze if uri.is_a?(URI::HTTP) && uri.host && !uri.host.empty? && uri.path.empty? && uri.query.nil? && uri.fragment.nil? && uri.userinfo.nil?
|
|
116
|
+
raise Invalid, "allowed_origins entries must be origins such as \"https://app.example\", without a path; got #{value.inspect}"
|
|
117
|
+
rescue URI::InvalidURIError
|
|
118
|
+
raise Invalid, "allowed_origins entries must be origins such as \"https://app.example\", without a path; got #{value.inspect}"
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def check_tool_verbs(value)
|
|
122
|
+
raise Invalid, "tool_verbs must be a hash of action to verb; got #{value.inspect}" unless value.is_a?(Hash)
|
|
123
|
+
verbs = DEFAULT_TOOL_VERBS.merge(value.to_h { |action, verb| [action.to_sym, verb] })
|
|
124
|
+
verbs.each { |action, verb| check_text(:"tool_verbs[:#{action}]", verb) }
|
|
125
|
+
verbs.transform_values { |verb| verb.dup.freeze }.freeze
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
module StatelessMcp
|
|
2
|
+
# What a tool says about itself in tools/list: its title, description,
|
|
3
|
+
# behaviour hints and arguments. The name is left to whatever routes calls
|
|
4
|
+
# to the tool, so one declaration can't be listed under two names by mistake.
|
|
5
|
+
#
|
|
6
|
+
# StatelessMcp::ToolDeclaration.new(title: "List notes", description: "Lists notes, newest first.") do
|
|
7
|
+
# argument :tag, :string, "Only notes with this tag"
|
|
8
|
+
# argument :created_after, :datetime, "ISO 8601 date or time"
|
|
9
|
+
# paginated "notes"
|
|
10
|
+
# end
|
|
11
|
+
#
|
|
12
|
+
# The arguments are built once per server, because paginated describes that
|
|
13
|
+
# server's page sizes.
|
|
14
|
+
class ToolDeclaration
|
|
15
|
+
READ_ONLY = { read_only_hint: true, destructive_hint: false, idempotent_hint: true, open_world_hint: false }.freeze
|
|
16
|
+
|
|
17
|
+
attr_reader :title, :description, :annotations
|
|
18
|
+
|
|
19
|
+
def initialize(title:, description:, annotations: READ_ONLY, &arguments)
|
|
20
|
+
@title = title
|
|
21
|
+
@description = description
|
|
22
|
+
@annotations = annotations
|
|
23
|
+
@arguments = arguments
|
|
24
|
+
@input_schemas = {}
|
|
25
|
+
@lock = Mutex.new
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# The JSON Schema the mcp gem validates a call's arguments with.
|
|
29
|
+
def input_schema(server)
|
|
30
|
+
@lock.synchronize { @input_schemas[server] ||= MCP::Tool::InputSchema.new(Arguments.build(server, &@arguments)) }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# The tools/list entry, built by the mcp gem so its shape keeps up with the spec.
|
|
34
|
+
def to_h(name, server)
|
|
35
|
+
MCP::Tool.define(name: name, title: title, description: description, input_schema: input_schema(server), annotations: annotations).to_h
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# The block a declaration is written in.
|
|
39
|
+
class Arguments
|
|
40
|
+
def self.build(server, &block)
|
|
41
|
+
arguments = new(server)
|
|
42
|
+
arguments.instance_eval(&block) if block
|
|
43
|
+
arguments.to_h
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def initialize(server)
|
|
47
|
+
@server = server
|
|
48
|
+
@properties = {}
|
|
49
|
+
@required = []
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Types are JSON Schema's, plus :datetime, which travels as a string.
|
|
53
|
+
# Further keywords, such as enum: or minimum:, go into the property as given.
|
|
54
|
+
def argument(name, type, description, required: false, **keywords)
|
|
55
|
+
@properties[name.to_s] = { type: type.to_s == "datetime" ? "string" : type.to_s, description: description, **keywords }
|
|
56
|
+
@required << name.to_s if required
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# limit and cursor, described with the server's page sizes.
|
|
60
|
+
def paginated(noun = "results")
|
|
61
|
+
argument :limit, :integer, "How many #{noun} to return, #{@server.page_size} by default", minimum: 1, maximum: @server.max_page_size
|
|
62
|
+
argument :cursor, :string, "next_cursor from an earlier result, for the next page"
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def to_h
|
|
66
|
+
{ properties: @properties, required: (@required unless @required.empty?) }.compact
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
require "base64"
|
|
2
|
+
require "json"
|
|
3
|
+
require "rack"
|
|
4
|
+
require "stringio"
|
|
5
|
+
require "uri"
|
|
6
|
+
require "mcp"
|
|
7
|
+
require "mcp/server/transports/streamable_http_transport"
|
|
8
|
+
require_relative "stateless_mcp/version"
|
|
9
|
+
|
|
10
|
+
# Serves the 2026-07-28 Model Context Protocol without sessions: every request
|
|
11
|
+
# carries its own protocol version and is answered on its own, so any process
|
|
12
|
+
# can take any call.
|
|
13
|
+
module StatelessMcp
|
|
14
|
+
# Raised with a message the model can act on, such as a date it couldn't
|
|
15
|
+
# read. It is answered as a refused tool call, which the model sees, rather
|
|
16
|
+
# than a protocol error, which it may not.
|
|
17
|
+
InvalidArgument = Class.new(StandardError)
|
|
18
|
+
|
|
19
|
+
# The parsed JSON-RPC body, which the middleware leaves in the Rack env.
|
|
20
|
+
REQUEST_ENV_KEY = "stateless_mcp.request"
|
|
21
|
+
|
|
22
|
+
# Methods whose target is mirrored into the Mcp-Name header.
|
|
23
|
+
NAME_BEARING_METHODS = MCP::Server::Transports::StreamableHTTPTransport::NAME_BEARING_METHODS
|
|
24
|
+
|
|
25
|
+
# The tool name, or resource URI, that a request's Mcp-Name header must repeat.
|
|
26
|
+
def self.target_name(body)
|
|
27
|
+
params = body["params"]
|
|
28
|
+
params["name"] || params["uri"] if NAME_BEARING_METHODS.include?(body["method"]) && params.is_a?(Hash)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
require_relative "stateless_mcp/rpc"
|
|
33
|
+
require_relative "stateless_mcp/refusal"
|
|
34
|
+
require_relative "stateless_mcp/server"
|
|
35
|
+
require_relative "stateless_mcp/cursor"
|
|
36
|
+
require_relative "stateless_mcp/pagination"
|
|
37
|
+
require_relative "stateless_mcp/request_body"
|
|
38
|
+
require_relative "stateless_mcp/header_check"
|
|
39
|
+
require_relative "stateless_mcp/middleware"
|
|
40
|
+
require_relative "stateless_mcp/result"
|
|
41
|
+
require_relative "stateless_mcp/tool_declaration"
|
metadata
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: stateless-mcp
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- James Edwards-Jones
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 2026-10-19 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: base64
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0.2'
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '1'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: '0.2'
|
|
29
|
+
- - "<"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '1'
|
|
32
|
+
- !ruby/object:Gem::Dependency
|
|
33
|
+
name: mcp
|
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - ">="
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '1.8'
|
|
39
|
+
- - "<"
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '2'
|
|
42
|
+
type: :runtime
|
|
43
|
+
prerelease: false
|
|
44
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ">="
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: '1.8'
|
|
49
|
+
- - "<"
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: '2'
|
|
52
|
+
- !ruby/object:Gem::Dependency
|
|
53
|
+
name: rack
|
|
54
|
+
requirement: !ruby/object:Gem::Requirement
|
|
55
|
+
requirements:
|
|
56
|
+
- - ">="
|
|
57
|
+
- !ruby/object:Gem::Version
|
|
58
|
+
version: '2.2'
|
|
59
|
+
- - "<"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '4'
|
|
62
|
+
type: :runtime
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '2.2'
|
|
69
|
+
- - "<"
|
|
70
|
+
- !ruby/object:Gem::Version
|
|
71
|
+
version: '4'
|
|
72
|
+
description: 'Serves the 2026-07-28 Model Context Protocol without sessions: every
|
|
73
|
+
request is answered on its own, so any instance behind a load balancer can take
|
|
74
|
+
any call. Rack middleware, JSON-RPC errors, cursors and tool schemas, with Rails
|
|
75
|
+
support in stateless-mcp-rails.'
|
|
76
|
+
email:
|
|
77
|
+
- git@jamedjo.co.uk
|
|
78
|
+
executables: []
|
|
79
|
+
extensions: []
|
|
80
|
+
extra_rdoc_files: []
|
|
81
|
+
files:
|
|
82
|
+
- CHANGELOG.md
|
|
83
|
+
- LICENSE-APACHE
|
|
84
|
+
- LICENSE-MIT
|
|
85
|
+
- README.md
|
|
86
|
+
- lib/stateless-mcp.rb
|
|
87
|
+
- lib/stateless_mcp.rb
|
|
88
|
+
- lib/stateless_mcp/cursor.rb
|
|
89
|
+
- lib/stateless_mcp/header_check.rb
|
|
90
|
+
- lib/stateless_mcp/middleware.rb
|
|
91
|
+
- lib/stateless_mcp/pagination.rb
|
|
92
|
+
- lib/stateless_mcp/refusal.rb
|
|
93
|
+
- lib/stateless_mcp/request_body.rb
|
|
94
|
+
- lib/stateless_mcp/result.rb
|
|
95
|
+
- lib/stateless_mcp/rpc.rb
|
|
96
|
+
- lib/stateless_mcp/server.rb
|
|
97
|
+
- lib/stateless_mcp/tool_declaration.rb
|
|
98
|
+
- lib/stateless_mcp/version.rb
|
|
99
|
+
homepage: https://github.com/Jamedjo/stateless-mcp
|
|
100
|
+
licenses:
|
|
101
|
+
- MIT
|
|
102
|
+
- Apache-2.0
|
|
103
|
+
metadata:
|
|
104
|
+
homepage_uri: https://github.com/Jamedjo/stateless-mcp
|
|
105
|
+
source_code_uri: https://github.com/Jamedjo/stateless-mcp/tree/v0.0.1/stateless-mcp
|
|
106
|
+
changelog_uri: https://github.com/Jamedjo/stateless-mcp/blob/main/stateless-mcp/CHANGELOG.md
|
|
107
|
+
documentation_uri: https://github.com/Jamedjo/stateless-mcp/blob/main/README.md
|
|
108
|
+
bug_tracker_uri: https://github.com/Jamedjo/stateless-mcp/issues
|
|
109
|
+
rubygems_mfa_required: 'true'
|
|
110
|
+
rdoc_options: []
|
|
111
|
+
require_paths:
|
|
112
|
+
- lib
|
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
114
|
+
requirements:
|
|
115
|
+
- - ">="
|
|
116
|
+
- !ruby/object:Gem::Version
|
|
117
|
+
version: '3.2'
|
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
|
+
requirements:
|
|
120
|
+
- - ">="
|
|
121
|
+
- !ruby/object:Gem::Version
|
|
122
|
+
version: '0'
|
|
123
|
+
requirements: []
|
|
124
|
+
rubygems_version: 3.6.7
|
|
125
|
+
specification_version: 4
|
|
126
|
+
summary: Stateless Model Context Protocol servers over plain HTTP, for any Rack app
|
|
127
|
+
test_files: []
|