Skip to content

secondsky/claude-skills / plugins/cloudflare-agents/skills/cloudflare-agents

cloudflare-agents

Verified

Build AI agents on Cloudflare Workers with MCP integration, tool use, and LLM providers.

templates/mcp-server-basic.ts

View on GitHub
// Model Context Protocol (MCP) Server with Agents SDK

import { McpAgent } from "agents/mcp";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";

interface Env {
  // Add bindings (AI, DB, etc.)
}

// Stateless MCP Server (basic tools)
export class BasicMCP extends McpAgent {
  server = new McpServer({
    name: "BasicMCP",
    version: "1.0.0"
  });

  async init() {
    // Tool 1: Add numbers
    this.server.tool(
      "add",
      "Add two numbers together",
      {
        a: z.number().describe("First number"),
        b: z.number().describe("Second number")
      },
      async ({ a, b }) => ({
        content: [{ type: "text", text: String(a + b) }]
      })
    );

    // Tool 2: Get current time
    this.server.tool(
      "get-time",
      "Get the current server time",
      {},
      async () => ({
        content: [{
          type: "text",
          text: new Date().toISOString()
        }]
      })
    );

    // Tool 3: Echo message
    this.server.tool(
      "echo",
      "Echo back a message",
      {
        message: z.string().describe("Message to echo")
      },
      async ({ message }) => ({
        content: [{ type: "text", text: message }]
      })
    );
  }
}

// Stateful MCP Server (with Agent state)
type CounterState = {
  counter: number;
  operations: string[];
};

export class StatefulMCP extends McpAgent<Env, CounterState> {
  server = new McpServer({
    name: "StatefulMCP",
    version: "1.0.0"
  });

  initialState: CounterState = {
    counter: 0,
    operations: []
  };

  async init() {
    // Resource: Counter value
    this.server.resource(
      "counter",
      "mcp://resource/counter",
      (uri) => ({
        contents: [{
          uri: uri.href,
          text: String(this.state.counter)
        }]
      })
    );

    // Resource: Operations history
    this.server.resource(
      "history",
      "mcp://resource/history",
      (uri) => ({
        contents: [{
          uri: uri.href,
          text: JSON.stringify(this.state.operations, null, 2)
        }]
      })
    );

    // Tool: Increment counter
    this.server.tool(
      "increment",
      "Increment the counter by a specified amount",
      {
        amount: z.number().describe("Amount to increment")
      },
      async ({ amount }) => {
        const newCounter = this.state.counter + amount;

        this.setState({
          counter: newCounter,
          operations: [
            ...this.state.operations,
            `increment(${amount}) = ${newCounter}`
          ]
        });

        return {
          content: [{
            type: "text",
            text: `Counter is now ${newCounter}`
          }]
        };
      }
    );

    // Tool: Reset counter
    this.server.tool(
      "reset",
      "Reset the counter to zero",
      {},
      async () => {
        this.setState({
          counter: 0,
          operations: [...this.state.operations, "reset() = 0"]
        });

        return {
          content: [{ type: "text", text: "Counter reset to 0" }]
        };
      }
    );
  }

  // React to state updates
  onStateUpdate(state: CounterState) {
    console.log('MCP State updated:', state);
  }
}

// Export with Hono for routing
import { Hono } from 'hono';

const app = new Hono();

// Mount MCP servers
// Streamable HTTP transport (modern, recommended)
app.mount('/mcp', BasicMCP.serve('/mcp').fetch, { replaceRequest: false });

// SSE transport (legacy, deprecated)
app.mount('/sse', BasicMCP.serveSSE('/sse').fetch, { replaceRequest: false });

// Stateful MCP server
app.mount('/stateful/mcp', StatefulMCP.serve('/stateful/mcp').fetch, { replaceRequest: false });

export default app;

// With OAuth (optional)
/*
import { OAuthProvider } from '@cloudflare/workers-oauth-provider';

export default new OAuthProvider({
  apiHandlers: {
    '/sse': BasicMCP.serveSSE('/sse'),
    '/mcp': BasicMCP.serve('/mcp')
  },
  clientId: 'your-client-id',
  clientSecret: 'your-client-secret',
  // ... other OAuth config
});
*/
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall