-
Notifications
You must be signed in to change notification settings - Fork 300
feat: agents mcp #643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: agents mcp #643
Changes from 6 commits
c93ff57
a626534
bcfb0e7
6da496f
861abad
a758ed3
502eb95
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| /* eslint-disable */ | ||
| // Generated by Wrangler by running `wrangler types env.d.ts --include-runtime false` (hash: 83be826bfd14e0006039e186e1b11835) | ||
| declare namespace Cloudflare { | ||
| interface Env { | ||
| DOCS_KV: KVNamespace; | ||
| } | ||
| } | ||
| interface Env extends Cloudflare.Env {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| GITHUB_TOKEN=optional-to-avoid-rate-limiting |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; | ||
| import { CfWorkerJsonSchemaValidator } from "@modelcontextprotocol/sdk/validation/cfworker"; | ||
| import { z } from "zod"; | ||
| import { createMcpHandler } from "agents/mcp"; | ||
| import { fetchAndBuildIndex, formatResults } from "./utils"; | ||
| import { search } from "@orama/orama"; | ||
| import { Effect } from "effect"; | ||
|
|
||
| // TODO: instrument this server for observability | ||
| const mcpServer = new McpServer( | ||
| { | ||
| name: "agents-mcp", | ||
| version: "0.0.1" | ||
| }, | ||
| { | ||
| capabilities: {}, | ||
| jsonSchemaValidator: new CfWorkerJsonSchemaValidator() | ||
| } | ||
| ); | ||
|
|
||
| const inputSchema = { | ||
| query: z | ||
| .string() | ||
| .describe( | ||
| "query string to search for eg. 'agent hibernate', 'schedule tasks'" | ||
| ), | ||
| k: z.number().optional().default(5).describe("number of results to return") | ||
| }; | ||
|
|
||
| mcpServer.registerTool( | ||
| "search-agent-docs", | ||
| { | ||
| description: | ||
| "Token efficient search of the Cloudflare Agents SDK documentation", | ||
| inputSchema | ||
| }, | ||
| async ({ query, k }) => { | ||
| const searchEffect = Effect.gen(function* () { | ||
| console.log({ query, k }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stray log?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'd kinda like to have some logging on the queries are being asked. We need to instrument the server properly but I added this till then. |
||
| const term = query.trim(); | ||
|
|
||
| const docsDb = yield* fetchAndBuildIndex; | ||
|
|
||
| const result = search(docsDb, { term, limit: k }); | ||
| const searchResult = yield* result instanceof Promise | ||
| ? Effect.promise(() => result) | ||
| : Effect.succeed(result); | ||
|
|
||
| return { | ||
| content: [ | ||
| { | ||
| type: "text" as const, | ||
| text: formatResults(searchResult, term, k) | ||
| } | ||
| ] | ||
| }; | ||
| }).pipe( | ||
| Effect.catchAll((error) => { | ||
| console.error(error); | ||
| return Effect.succeed({ | ||
| content: [ | ||
| { | ||
| type: "text" as const, | ||
| text: `There was an error with the search tool. Please try again later.` | ||
| } | ||
| ] | ||
| }); | ||
| }) | ||
| ); | ||
|
|
||
| return await Effect.runPromise(searchEffect); | ||
| } | ||
| ); | ||
|
|
||
| export default { | ||
| async fetch(request: Request, env: Env, ctx: ExecutionContext) { | ||
| return createMcpHandler(mcpServer)(request, env, ctx); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can make a scheduled worker here and run this everyday |
||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sneaking in effect huh