Xava Labs MCP Template

Xava Labs MCP Template

A template repository for building Model Context Protocol (MCP) servers that enables developers to create interactive AI agents with real-time bidirectional communication capabilities through WebSocket and SSE endpoints.

Category
访问服务器

README

Xava Labs Typescript MCP Template

A template repository for bootstrapping MCPs (Model Context Protocol) for the xava-labs/typescript-agent-framework.

Deploy to Cloudflare

Getting Started

Setup the repository

Option A: Use this template*

  1. Click the "Use this template" button at the top of this repository
  2. Clone your new repository

Option b: Use wrangler init

You can create a new project based on this template using wrangler:

npx wrangler init my-mcp-project --git https://github.com/xava-labs/mcp-template
cd my-mcp-project

Once you have completed one of the above methods, then run the following commands in your terminal to get started:

yarn install
yarn dev

The above will boostrap a serverless cloudflare compatible MCP Server with the following urls:

  • /ws - Websocket connection endpoint
  • /sse - SSE connection endpoint

Features

  • WebSocket Client Support: Includes official WebSocket client for real-time bidirectional communication
  • SSE Client Support: Includes Server-Sent Events client for server-to-client streaming
  • MCP Inspector: Debug and monitor your MCP during development
  • Cloudflare Workers Integration: Built on Cloudflare Workers for edge computing capabilities
  • Integration Testing Suite: Websocket and SSE testing tools to do full integration testing with local miniflare services (D1/KV/etc) for ease of testing features without mocking.

Available Scripts

  • yarn dev: Runs both the MCP Inspector (port 6274) and Cloudflare Worker (port 8787) concurrently
  • yarn start: Runs only the Cloudflare Worker (port 8787)
  • yarn test: Runs tests with Vitest
  • yarn deploy: Deploys your MCP to Cloudflare Workers
  • yarn cf-typegen: Generates TypeScript types for Cloudflare Workers (run this everytime you add new changes to wrangler.jsonc)

Development

This template implements an MCP server using Durable Objects for stateful connections. The base project structure offers two main approaches for extending functionality:

McpHonoServerDO Implementation

By default, the template uses McpHonoServerDO which combines the MCP server with Hono, a fast and lightweight web framework. This provides a clean routing system and middleware capabilities.

Extending with Tools, Resources, and Prompts

The main server implementation is in src/server.ts and extends McpHonoServerDO:

export class ExampleMcpServer extends McpHonoServerDO {
  // Required abstract method implementation
  getImplementation(): Implementation {
    return {
      name: 'ExampleMcpServer',
      version: '1.0.0',
    };
  }

  // Configure server by adding tools, resources, and prompts
  configureServer(server: McpServer): void {
    setupServerTools(server);
    setupServerResources(server);
    setupServerPrompts(server);
  }
}

To add functionality, use the following modules:

  1. Tools (src/tools.ts): Define functions that clients can call
export function setupServerTools(server: McpServer) {
  server.tool(
    'tool_name',           // Name of the tool
    'Tool description',    // Description
    {                      // Parameters schema using zod
      param1: z.string().describe('Parameter description'),
    },       
    async ({ param1 }) => {
      // Tool implementation
      return {
        content: [
          {
            type: "text",
            text: `Result: ${param1}`
          }
        ]
      };
    }
  );
}
  1. Resources (src/resources.ts): Define persistent resources clients can access
export function setupServerResources(server: McpServer) {
  server.resource(
    'resource_name',
    'resource://path/{id}',
    async (uri: URL) => {
      // Resource implementation
      return {
        contents: [
          {
            text: `Resource data`,
            uri: uri.href
          }
        ]
      };
    }
  );
}
  1. Prompts (src/prompts.ts): Define prompt templates
export function setupServerPrompts(server: McpServer) {
  server.prompt(
    'prompt_name',
    'Prompt description',
    () => ({
      messages: [{
        role: 'assistant',
        content: {
          type: 'text',
          text: `Your prompt text here`
        }
      }]
    })
  );
}

Customizing Routes with Hono

To add custom HTTP endpoints with McpHonoServerDO, extend the setupRoutes method:

export class ExampleMcpServer extends McpHonoServerDO {
  // Other methods...

  protected setupRoutes(app: Hono<{ Bindings: Env }>): void {
    // Call the parent implementation to set up MCP routes
    super.setupRoutes(app);
    
    // Add your custom routes
    app.get('/api/status', (c) => {
      return c.json({ status: 'ok' });
    });
    
    app.post('/api/data', async (c) => {
      const body = await c.req.json();
      // Process data
      return c.json({ success: true });
    });
  }
}

McpServerDO Implementation (Native Cloudflare Routing)

If you need more control over the HTTP request handling, you can directly extend McpServerDO instead. This gives you full control over the fetch method:

export class CustomMcpServer extends McpServerDO {
  // Required abstract method implementations
  getImplementation(): Implementation {
    return {
      name: 'CustomMcpServer',
      version: '1.0.0',
    };
  }
  
  configureServer(server: McpServer): void {
    setupServerTools(server);
    setupServerResources(server);
    setupServerPrompts(server);
  }
  
  // Override the fetch method for complete control over routing
  async fetch(request: Request): Promise<Response> {
    const url = new URL(request.url);
    const path = url.pathname;
    
    // Handle custom routes
    if (path === '/api/custom') {
      return new Response(JSON.stringify({ custom: true }), {
        headers: { 'Content-Type': 'application/json' }
      });
    }
    
    // Pass through MCP-related requests to the parent implementation
    return super.fetch(request);
  }
}

This approach is useful when you need to:

  • Handle specific routes with custom logic
  • Implement complex middleware or authentication
  • Intercept or modify requests before they reach the MCP handler
  • Add custom WebSocket or SSE endpoints beyond the standard MCP implementation

Examples

CRUD Todo List Example

For a complete working example, check out the CRUD Todo List MCP Example which demonstrates:

  • Full CRUD operations using MCP tools
  • SQLite database integration for persistence
  • Real-time updates via WebSocket/SSE
  • Comprehensive error handling
  • Advanced filtering and sorting capabilities
  • Rich prompts and resources

Related Resources

Core Packages

Documentation

  • Documentation: Coming soon!

Community

Join our community to get help, share ideas, and contribute to the project:

  • Discord: Join the #mcp channel for feature requests, support, and discussions

Contributing

We welcome contributions to improve this template! Here's how you can contribute:

  1. Fork the repository: Create a fork to make your changes

  2. Create a branch: Make your changes in a new branch

    git checkout -b feature/your-feature-name
    
  3. Commit your changes: Make meaningful commits

    git commit -m "Add feature: brief description"
    
  4. Push to your fork: Push your changes to your fork

    git push origin feature/your-feature-name
    
  5. Create a pull request: Open a PR with a detailed description of your changes

Pull Request Guidelines

  • Provide a clear, descriptive title for your PR
  • Include a detailed description of what your PR does
  • Reference any related issues
  • Include screenshots or examples if applicable
  • Ensure all tests pass
  • Keep PRs focused on a single feature or fix

For larger changes or features, we recommend discussing them first in our Discord channel to ensure alignment with the project direction.

Deployment

To deploy your MCP to Cloudflare Workers, run:

yarn deploy

Or use the Deploy to Cloudflare button above to deploy directly from GitHub.

License

MIT

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选