mettr 0.1.0 → 0.2.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 +4 -4
- data/README.md +26 -0
- data/lib/mettr/contract.rb +14 -0
- data/lib/mettr/mcp.rb +264 -0
- data/lib/mettr/rack.rb +3 -0
- data/lib/mettr/version.rb +1 -1
- data/lib/mettr.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: bd60d23a48e64563814840acc1771d9c9002f843f1c934be624c3b1f6c67d7f3
|
|
4
|
+
data.tar.gz: 8b1d12d96786f276b33e498c4ff9b728f2b21ae00b5dabfde68b3e31ec77e084
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e91d5f5c88e860e535e04fba9366ddb516002ea3ec3103fdeb7d1efb428cb6b6d0709cbf8cfae855cc2e18a128ecee276d94dd600f62ffb963085784a47b681d
|
|
7
|
+
data.tar.gz: 5db7fbceb771d8f1557644e276d64097d964f58d9695e6f83dc81ae291b4b41cf05dd5e6d6e6f7225cc315bf97f1ee32f0c850c431746c0c716f3f32a78246c3
|
data/README.md
CHANGED
|
@@ -90,6 +90,32 @@ Mettr.subscription(
|
|
|
90
90
|
)
|
|
91
91
|
```
|
|
92
92
|
|
|
93
|
+
## Your MCP server
|
|
94
|
+
|
|
95
|
+
If your app runs an MCP server, mettr shows how AI assistants use it:
|
|
96
|
+
calls, tools, clients, failures and time. Put `Mettr::Mcp` before the
|
|
97
|
+
server's own auth, so a 401 that sends a client to sign in counts too:
|
|
98
|
+
|
|
99
|
+
```ruby
|
|
100
|
+
# Rails: config/application.rb
|
|
101
|
+
config.middleware.use Mettr::Mcp, path: "/mcp" # errors: true keeps failed calls' messages
|
|
102
|
+
|
|
103
|
+
# Rack
|
|
104
|
+
use Mettr::Mcp, path: "/mcp", user: ->(env) { env["warden"]&.user&.id }
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
`path:` takes a string, a regexp, or `nil` for every request that reaches
|
|
108
|
+
it. The user is read once the server has answered (Mettr.configure's
|
|
109
|
+
`user`, else Warden's). Each handshake, tool call, resource read and
|
|
110
|
+
prompt becomes an `mcp.*` event with the client (Claude, ChatGPT,
|
|
111
|
+
Cursor...), how it ended and how long it took. Only the tool's,
|
|
112
|
+
resource's or prompt's name is sent, never the arguments or the result,
|
|
113
|
+
and nothing of the assistant's address or user agent. A failed call's
|
|
114
|
+
message is sent only with `errors: true` (it may quote the user), cut to
|
|
115
|
+
200 characters; the session id only as a hash. `Mettr::Rack` sends no
|
|
116
|
+
crawl, 404 or timing for the path. Read with Pro: see
|
|
117
|
+
[MCP analytics](https://mettr.app/docs/mcp-analytics).
|
|
118
|
+
|
|
93
119
|
## History and deploys
|
|
94
120
|
|
|
95
121
|
```ruby
|
data/lib/mettr/contract.rb
CHANGED
|
@@ -32,6 +32,20 @@ module Mettr
|
|
|
32
32
|
SCRIPT_CACHE_SECONDS = 3600
|
|
33
33
|
SCRIPT_RETRY_SECONDS = 60
|
|
34
34
|
|
|
35
|
+
# Calls to the customer's own MCP server (Mettr::Mcp).
|
|
36
|
+
MCP_METHODS = {
|
|
37
|
+
"initialize" => "mcp.connect",
|
|
38
|
+
"server/discover" => "mcp.connect",
|
|
39
|
+
"tools/call" => "mcp.tool",
|
|
40
|
+
"resources/read" => "mcp.resource",
|
|
41
|
+
"prompts/get" => "mcp.prompt"
|
|
42
|
+
}.freeze
|
|
43
|
+
MCP_PROPS = %w[tool resource prompt client client_version protocol outcome status duration_ms error_code error session].freeze
|
|
44
|
+
MCP_OUTCOMES = %w[ok tool_error error sign_in forbidden throttled failed].freeze
|
|
45
|
+
MCP_ERROR_LENGTH = 200
|
|
46
|
+
MCP_META = {client_info: "io.modelcontextprotocol/clientInfo", protocol_version: "io.modelcontextprotocol/protocolVersion"}.freeze
|
|
47
|
+
MCP_SESSION_HEADER = "Mcp-Session-Id"
|
|
48
|
+
|
|
35
49
|
TIMEOUT_SECONDS = 3
|
|
36
50
|
RESPONSE_TIME_SAMPLE = 0.05
|
|
37
51
|
|
data/lib/mettr/mcp.rb
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require "json"
|
|
5
|
+
require "stringio"
|
|
6
|
+
|
|
7
|
+
module Mettr
|
|
8
|
+
# Calls to your own MCP server, for mettr's MCP panels. Rack middleware for
|
|
9
|
+
# the server's path, outside its auth, so the 401 that sends a client to
|
|
10
|
+
# sign in is counted too:
|
|
11
|
+
#
|
|
12
|
+
# use Mettr::Mcp, path: "/mcp", user: ->(env) { env["warden"]&.user&.id }
|
|
13
|
+
#
|
|
14
|
+
# Each call an AI assistant makes (the handshake, a tool, a resource, a
|
|
15
|
+
# prompt) is sent as an mcp.* event with the client, how the call ended and
|
|
16
|
+
# how long it took. What a call was about is sent (the tool's, resource's
|
|
17
|
+
# or prompt's name), never what it carried or what came back; a failed
|
|
18
|
+
# call's message only with errors: true (it may quote the user). Nothing of
|
|
19
|
+
# the client's address or user agent is sent: they are the assistant's
|
|
20
|
+
# servers', not the person's. Lists and pings are not sent. Mettr::Rack
|
|
21
|
+
# sends no crawl, 404 or timing for the path.
|
|
22
|
+
class Mcp
|
|
23
|
+
ENV_KEY = "mettr.mcp"
|
|
24
|
+
|
|
25
|
+
TARGETS = {"mcp.tool" => "tool", "mcp.resource" => "resource", "mcp.prompt" => "prompt"}.freeze
|
|
26
|
+
|
|
27
|
+
# How many older clients' sessions are remembered at once.
|
|
28
|
+
SESSIONS = 10_000
|
|
29
|
+
|
|
30
|
+
# How much of an answer is read for its outcome; the rest still goes to the client.
|
|
31
|
+
MAX_ANSWER = 1_048_576
|
|
32
|
+
|
|
33
|
+
# path: the MCP server's path (a String, a Regexp, or nil for every
|
|
34
|
+
# request that reaches the middleware); user: the signed-in user's id
|
|
35
|
+
# from the Rack env, read once the server has answered (Mettr.configure's,
|
|
36
|
+
# else Warden's); staff: whether the request is staff's; errors: send a
|
|
37
|
+
# failed call's message.
|
|
38
|
+
def initialize(app, client: nil, path: "/mcp", user: nil, staff: nil, errors: false)
|
|
39
|
+
@app = app
|
|
40
|
+
@client = client
|
|
41
|
+
@path = path
|
|
42
|
+
@user = user
|
|
43
|
+
@staff = staff
|
|
44
|
+
@errors = errors
|
|
45
|
+
@clients = {}
|
|
46
|
+
@mutex = Mutex.new
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def call(env)
|
|
50
|
+
return @app.call(env) unless mcp?(env)
|
|
51
|
+
|
|
52
|
+
env[ENV_KEY] = true
|
|
53
|
+
return @app.call(env) unless env["REQUEST_METHOD"] == "POST"
|
|
54
|
+
|
|
55
|
+
raw = env["rack.input"]&.read.to_s
|
|
56
|
+
env["rack.input"] = StringIO.new(raw)
|
|
57
|
+
calls = messages(raw).select do |message|
|
|
58
|
+
message.is_a?(Hash) && !message["id"].nil? && Contract::MCP_METHODS.key?(message["method"])
|
|
59
|
+
end
|
|
60
|
+
return @app.call(env) if calls.empty?
|
|
61
|
+
|
|
62
|
+
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
63
|
+
|
|
64
|
+
begin
|
|
65
|
+
status, headers, body = @app.call(env)
|
|
66
|
+
rescue Exception # rubocop:disable Lint/RescueException -- counted, then raised again
|
|
67
|
+
record(env, calls, 500, {}, "", started)
|
|
68
|
+
raise
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
done = ->(answer) { record(env, calls, status.to_i, headers, answer, started) }
|
|
72
|
+
|
|
73
|
+
# A streaming body (Rack 3's call) is not read: the call goes without its outcome.
|
|
74
|
+
unless body.respond_to?(:each)
|
|
75
|
+
done.call("")
|
|
76
|
+
return [status, headers, body]
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
[status, headers, Answer.new(body, done)]
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# The answer as it goes to the client, kept up to MAX_ANSWER; the calls
|
|
83
|
+
# are sent once it is closed.
|
|
84
|
+
class Answer
|
|
85
|
+
def initialize(body, done)
|
|
86
|
+
@body = body
|
|
87
|
+
@done = done
|
|
88
|
+
@kept = +""
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def each
|
|
92
|
+
@body.each do |chunk|
|
|
93
|
+
@kept << chunk.to_s.b if @kept.bytesize < MAX_ANSWER
|
|
94
|
+
yield chunk
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def close
|
|
99
|
+
@body.close if @body.respond_to?(:close)
|
|
100
|
+
ensure
|
|
101
|
+
done = @done
|
|
102
|
+
@done = nil
|
|
103
|
+
done&.call(@kept)
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
private
|
|
108
|
+
|
|
109
|
+
def mcp?(env)
|
|
110
|
+
return true if @path.nil?
|
|
111
|
+
|
|
112
|
+
path = "#{env["SCRIPT_NAME"]}#{env["PATH_INFO"]}"
|
|
113
|
+
@path.is_a?(Regexp) ? @path.match?(path) : path.chomp("/") == @path.chomp("/")
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def record(env, calls, status, headers, answer, started)
|
|
117
|
+
ms = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) * 1000
|
|
118
|
+
out = headers.to_h { |name, value| [name.to_s.downcase, Array(value).first.to_s] }
|
|
119
|
+
answers = answers(answer, out.fetch("content-type", ""))
|
|
120
|
+
session = [env["HTTP_MCP_SESSION_ID"], out["mcp-session-id"]].find { |value| !value.to_s.empty? }
|
|
121
|
+
hashed = session && Digest::SHA256.hexdigest(session)
|
|
122
|
+
visitor = Visitor.new(user_id: user(env)&.to_s, staff: staff(env))
|
|
123
|
+
client = @client || Mettr.client
|
|
124
|
+
|
|
125
|
+
calls.each do |call|
|
|
126
|
+
client.record(event(call, env, status, answers, ms, hashed), visitor)
|
|
127
|
+
end
|
|
128
|
+
rescue
|
|
129
|
+
# Counting never stands in the call's way.
|
|
130
|
+
nil
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def event(call, env, status, answers, ms, hashed)
|
|
134
|
+
method = call["method"]
|
|
135
|
+
name = Contract::MCP_METHODS[method]
|
|
136
|
+
params = object(call["params"])
|
|
137
|
+
meta = object(params["_meta"])
|
|
138
|
+
said, version = client_of(method, params, meta, hashed, env["HTTP_USER_AGENT"].to_s)
|
|
139
|
+
target = TARGETS[name]
|
|
140
|
+
what = text((method == "resources/read") ? params["uri"] : params["name"], 200)
|
|
141
|
+
what = what.sub(/[?#].*\z/m, "") if what && method == "resources/read"
|
|
142
|
+
protocol = meta[Contract::MCP_META[:protocol_version]] ||
|
|
143
|
+
((method == "initialize") ? params["protocolVersion"] : nil) || env["HTTP_MCP_PROTOCOL_VERSION"]
|
|
144
|
+
|
|
145
|
+
props = {
|
|
146
|
+
**((target && what && !what.empty?) ? {target => what} : {}),
|
|
147
|
+
"client" => said,
|
|
148
|
+
"client_version" => version,
|
|
149
|
+
"protocol" => text(protocol, 20),
|
|
150
|
+
**outcome(status, answers[key(call["id"])]),
|
|
151
|
+
"duration_ms" => ms.round,
|
|
152
|
+
# Names the visitor when no user is signed in; never the session itself.
|
|
153
|
+
"session" => hashed
|
|
154
|
+
}.compact
|
|
155
|
+
|
|
156
|
+
{"type" => "event", "name" => name, "url" => nil, "props" => props}
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# How the call ended: refused at the door (sign_in, forbidden, throttled,
|
|
160
|
+
# failed, with the status), a JSON-RPC error, the tool's own error, or ok.
|
|
161
|
+
def outcome(status, answer)
|
|
162
|
+
failed = (status >= 400) ? {"status" => status} : {}
|
|
163
|
+
error = answer&.fetch("error", nil)
|
|
164
|
+
|
|
165
|
+
if error.is_a?(Hash)
|
|
166
|
+
code = error["code"]
|
|
167
|
+
return {"outcome" => "error", **failed, "error_code" => code.is_a?(Integer) ? code : nil, "error" => message(error["message"])}
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
if status >= 400
|
|
171
|
+
named = {401 => "sign_in", 403 => "forbidden", 429 => "throttled"}
|
|
172
|
+
return {"outcome" => named.fetch(status, "failed"), **failed}
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
return {} if answer.nil?
|
|
176
|
+
|
|
177
|
+
result = object(answer["result"])
|
|
178
|
+
return {"outcome" => "ok"} unless result["isError"] == true
|
|
179
|
+
|
|
180
|
+
content = result["content"]
|
|
181
|
+
first = object(content.is_a?(Array) ? content.first : nil)
|
|
182
|
+
|
|
183
|
+
{"outcome" => "tool_error", "error" => message(first["text"])}
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def message(value)
|
|
187
|
+
return nil unless @errors && value.is_a?(String)
|
|
188
|
+
|
|
189
|
+
kept = value.strip[0, Contract::MCP_ERROR_LENGTH]
|
|
190
|
+
kept.empty? ? nil : kept
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
# The client by what it said of itself: in each request since MCP
|
|
194
|
+
# 2026-07-28, in its initialize before (remembered for its session), else
|
|
195
|
+
# the first word of its user agent ("Claude-User", "Cursor").
|
|
196
|
+
def client_of(method, params, meta, session, user_agent)
|
|
197
|
+
info = object(meta[Contract::MCP_META[:client_info]] || ((method == "initialize") ? params["clientInfo"] : nil))
|
|
198
|
+
name = text(info["name"], 100)
|
|
199
|
+
|
|
200
|
+
unless name.nil?
|
|
201
|
+
said = [name, text(info["version"], 50)]
|
|
202
|
+
remember(session, said) unless session.nil?
|
|
203
|
+
return said
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
known = session && @mutex.synchronize { @clients[session] }
|
|
207
|
+
return known unless known.nil?
|
|
208
|
+
|
|
209
|
+
[user_agent[%r{\A[^\s/;(]+}]&.slice(0, 100), nil]
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
def remember(session, said)
|
|
213
|
+
@mutex.synchronize do
|
|
214
|
+
@clients.delete(session)
|
|
215
|
+
@clients[session] = said
|
|
216
|
+
@clients.shift while @clients.size > SESSIONS
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
# The JSON-RPC answers by the id they answer, from JSON or a stream of events.
|
|
221
|
+
def answers(raw, content_type)
|
|
222
|
+
found = if content_type.start_with?("text/event-stream")
|
|
223
|
+
lines = String.new(raw, encoding: Encoding::UTF_8).scrub.each_line
|
|
224
|
+
lines.select { |line| line.start_with?("data:") }.flat_map { |line| messages(line[5..].strip) }
|
|
225
|
+
else
|
|
226
|
+
messages(raw)
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
found.select { |answer| answer.is_a?(Hash) && answer.key?("id") }.to_h { |answer| [key(answer["id"]), answer] }
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def messages(raw)
|
|
233
|
+
body = JSON.parse(raw)
|
|
234
|
+
body.is_a?(Array) ? body : [body]
|
|
235
|
+
rescue JSON::ParserError, EncodingError
|
|
236
|
+
[]
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
def user(env)
|
|
240
|
+
reader = @user || Mettr.configuration.user
|
|
241
|
+
return reader.call(env) unless reader.nil?
|
|
242
|
+
|
|
243
|
+
user = env["warden"]&.user
|
|
244
|
+
user.respond_to?(:id) ? user.id : nil
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
def staff(env)
|
|
248
|
+
reader = @staff || Mettr.configuration.staff
|
|
249
|
+
reader ? reader.call(env) : false
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
def object(value)
|
|
253
|
+
value.is_a?(Hash) ? value : {}
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
def text(value, length)
|
|
257
|
+
(value.is_a?(String) && !value.empty?) ? value[0, length] : nil
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
def key(id)
|
|
261
|
+
JSON.generate(id)
|
|
262
|
+
end
|
|
263
|
+
end
|
|
264
|
+
end
|
data/lib/mettr/rack.rb
CHANGED
|
@@ -81,6 +81,9 @@ module Mettr
|
|
|
81
81
|
end
|
|
82
82
|
|
|
83
83
|
def finish(client, env, status, started)
|
|
84
|
+
# An MCP server's requests are its clients' calls (Mettr::Mcp), not page visits.
|
|
85
|
+
return if env[Mettr::Mcp::ENV_KEY]
|
|
86
|
+
|
|
84
87
|
client.finished(
|
|
85
88
|
status: status.to_i,
|
|
86
89
|
ms: (Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) * 1000,
|
data/lib/mettr/version.rb
CHANGED
data/lib/mettr.rb
CHANGED
|
@@ -6,6 +6,7 @@ require "mettr/ip"
|
|
|
6
6
|
require "mettr/transport"
|
|
7
7
|
require "mettr/client"
|
|
8
8
|
require "mettr/rack"
|
|
9
|
+
require "mettr/mcp"
|
|
9
10
|
|
|
10
11
|
# mettr for Ruby: live analytics for websites and SaaS products. Mettr.client
|
|
11
12
|
# sends the app's own events, crawlers' visits, 404s, exceptions and
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mettr
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- mettr
|
|
@@ -22,6 +22,7 @@ files:
|
|
|
22
22
|
- lib/mettr/client.rb
|
|
23
23
|
- lib/mettr/contract.rb
|
|
24
24
|
- lib/mettr/ip.rb
|
|
25
|
+
- lib/mettr/mcp.rb
|
|
25
26
|
- lib/mettr/rack.rb
|
|
26
27
|
- lib/mettr/rails.rb
|
|
27
28
|
- lib/mettr/transport.rb
|