import { McpAgent } from "agents/mcp";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { Ai } from "@cloudflare/ai";
import { z } from "zod";
// Define our MCP agent with tools
export class MyMCP extends McpAgent {
server = new McpServer({
name: "Authless Calculator and Translator",
version: "1.1.0",
});
async init() {
// Simple addition tool
this.server.tool(
"add",
{ a: z.number(), b: z.number() },
async ({ a, b }) => ({
content: [{ type: "text", text: String(a + b) }],
})
);
// Calculator tool with multiple operations
this.server.tool(
"calculate",
{
operation: z.enum(["add", "subtract", "multiply", "divide"]),
a: z.number(),
b: z.number(),
},
async ({ operation, a, b }) => {
let result: number;
switch (operation) {
case "add":
result = a + b;
break;
case "subtract":
result = a - b;
break;
case "multiply":
result = a * b;
break;
case "divide":
if (b === 0)
return {
content: [
{
type: "text",
text: "Error: Cannot divide by zero",
},
],
};
result = a / b;
break;
}
return { content: [{ type: "text", text: String(result) }] };
}
);
// Translate various document types to the target language
this.server.tool(
"translate_document",
{
file: z.string(),
filename: z.string(),
targetLang: z.string(),
},
async ({ file, filename, targetLang }) => {
// biome-ignore lint/suspicious/noExplicitAny: Workers AI binding lacks types
const ai = new (Ai as any)((this as any).env.AI);
const blob = new Blob([Buffer.from(file, "base64")]);
// biome-ignore lint/suspicious/noExplicitAny: Workers AI binding lacks types
const markdown = await (ai as any).toMarkdown({ name: filename, blob });
let text = markdown.data;
if (markdown.mimeType.startsWith("image/")) {
// biome-ignore lint/suspicious/noExplicitAny: Workers AI binding lacks types
const ocr = await (ai as any).run("@cf/llava-hf/llava-1.5-7b-hf", {
image: file,
prompt: "Extract all text from this image",
});
text = ocr.description;
}
// biome-ignore lint/suspicious/noExplicitAny: Workers AI binding lacks types
const translated = await (ai as any).run("@cf/meta/m2m100-1.2b", {
text,
source_lang: "auto",
target_lang: targetLang,
});
return {
content: [
{
type: "text",
text: translated.translated_text || "",
},
],
};
}
);
}
}
export default {
fetch(request: Request, env: Env, ctx: ExecutionContext) {
const url = new URL(request.url);
if (url.pathname === "/sse" || url.pathname === "/sse/message") {
return MyMCP.serveSSE("/sse").fetch(request, env, ctx);
}
if (url.pathname === "/mcp") {
return MyMCP.serve("/mcp").fetch(request, env, ctx);
}
return new Response("Not found", { status: 404 });
},
};