-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsurface-1-mcp-agent.ts
More file actions
87 lines (81 loc) · 3.12 KB
/
Copy pathsurface-1-mcp-agent.ts
File metadata and controls
87 lines (81 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* atrib + Cloudflare Agents. Surface 1: McpAgent server-side instrumentation
*
* This is a Cloudflare Worker that exposes an MCP server using Cloudflare's
* `agents/mcp` package. Each MCP session gets its own Durable Object instance
* (a `WeatherMcp` here) and atrib middleware is applied to that instance's
* `this.server` in `init()`. Every successful tools/call going through that DO
* emits a signed attribution record.
*
* Deploy with:
* wrangler deploy
*
* Required wrangler.toml bindings:
* - Durable Object class binding for `WeatherMcp`
* - Secrets: ATRIB_PRIVATE_KEY, ATRIB_LOG_ENDPOINT
*
* NOTE: This file imports from `agents/mcp`, `@modelcontextprotocol/sdk`, and
* `zod`, none of which are dependencies of @atrib/integration. To run this
* example, copy it to a Worker project and install:
*
* pnpm add agents @modelcontextprotocol/sdk zod @atrib/mcp
*
* The integration package's tsconfig excludes `examples/` from compilation
* for exactly this reason. the examples typecheck against user-installed
* versions, not against our test build.
*/
import { McpAgent } from 'agents/mcp'
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
import { atrib } from '@atrib/mcp'
import { z } from 'zod'
interface Env {
ATRIB_PRIVATE_KEY: string
ATRIB_LOG_ENDPOINT: string
}
export class WeatherMcp extends McpAgent<Env> {
// `this.server` is a real McpServer from @modelcontextprotocol/sdk.
// the same class @atrib/mcp's atrib() middleware wraps.
server = new McpServer({ name: 'weather', version: '1.0.0' })
async init() {
// 1. Register tools as you normally would for an McpAgent.
this.server.registerTool(
'get_temperature',
{
description: 'Get the current temperature for a location',
inputSchema: {
latitude: z.number().describe('Latitude coordinate'),
longitude: z.number().describe('Longitude coordinate'),
},
},
async ({ latitude, longitude }) => {
const r = await fetch(
`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}¤t=temperature_2m&temperature_unit=fahrenheit`,
)
const data = (await r.json()) as {
current: { temperature_2m: number }
}
return {
content: [
{
type: 'text',
text: `Temperature: ${data.current.temperature_2m}°F`,
},
],
}
},
)
// 2. ★ ATRIB ★
// Apply attribution middleware to the in-process McpServer. This patches
// the dispatch path so every successful tools/call emits a signed record.
// The middleware is safe to call AFTER registerTool. see the retroactive
// wrap logic in @atrib/mcp middleware.ts (it rewrites an
// already-installed dispatcher in place).
atrib(this.server, {
creatorKey: this.env.ATRIB_PRIVATE_KEY,
serverUrl: 'https://your-worker.workers.dev/mcp',
logEndpoint: this.env.ATRIB_LOG_ENDPOINT,
})
}
}
// Export the Worker handler. McpAgent.serve handles the HTTP→DO routing.
export default WeatherMcp.serve('/mcp', { binding: 'WeatherMcp' })