🔥 HOT TOPIC

MCP Server Example

Build a Model Context Protocol (MCP) server with MoroJS to enable AI agents like Claude Desktop to interact with your applications. Perfect for the growing AI agent ecosystem!

What is Model Context Protocol (MCP)?

MCP is an open protocol by Anthropic that enables AI models like Claude to securely connect to external tools and data sources. Think of it as the "USB for AI agents" - a standardized way for AI to interact with your applications.

  • Connect Claude Desktop to your MoroJS applications
  • Enable AI agents to perform actions in your systems
  • Secure, standardized protocol for AI-application integration

Enterprise MCP Architecture

Modular Design

Clean separation with dedicated modules for tasks, weather, system info, and MCP protocol handling.

Dual Transport

STDIO for AI agent integration and HTTP for debugging - perfect for development and production.

Type Safety

Full TypeScript support with Zod schemas for request validation and comprehensive error handling.

MCP Server Implementation

Main Server Setup

typescript

1#!/usr/bin/env node
2
3// MCP Server with MoroJS - Enterprise Module Structure
4import { createApp } from '@morojs/moro';
5import { Server } from '@modelcontextprotocol/sdk/server/index.js';
6import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
7
8// Import enterprise modules
9import TasksModule from './modules/tasks';
10import WeatherModule from './modules/weather';
11import SystemModule from './modules/system';
12import MCPCoreModule from './modules/mcp-core';
13
14async function createMCPServer() {
15  // Create MoroJS app with enterprise configuration
16  const app = createApp({
17    cors: true,
18    compression: true,
19    helmet: true
20  });
21
22  // Mock database for demonstration
23  const mockDatabase = {
24    tasks: [],
25    weather: {},
26    system: {}
27  };
28
29  // Register database and event system
30  app.database(mockDatabase);
31
32  // Load enterprise modules
33  await app.loadModule(TasksModule);
34  await app.loadModule(WeatherModule); 
35  await app.loadModule(SystemModule);
36  await app.loadModule(MCPCoreModule);
37
38  return app;
39}

MCP Protocol Integration

typescript

1async function startMCPMode() {
2  console.error('Starting MoroJS MCP Server in MCP mode...');
3  
4  // Create context with database and events
5  const context = {
6    database: { tasks: [], weather: {}, system: {} },
7    events: { 
8      emit: async (event: string, data: any) => 
9        console.error(`Event: ${event}`, data) 
10    }
11  };
12
13  // Create and start MCP server
14  const mcpServer = await createMCPProtocolServer(context);
15  const transport = new StdioServerTransport();
16  await mcpServer.connect(transport);
17  
18  console.error('MCP Server connected via stdio transport');
19  console.error('Server ready for AI agent connections');
20}
21
22// Main execution - supports both MCP and HTTP modes
23async function main() {
24  const mode = process.argv[2] || 'mcp';
25
26  if (mode === 'http' || mode === 'debug') {
27    // HTTP mode for debugging
28    await startHTTPMode(3010);
29  } else {
30    // MCP mode (default) for AI agent integration
31    await startMCPMode();
32  }
33}

Available MCP Tools

Task Management Tools

  • create-task - Create new tasks with priority levels
  • list-tasks - List and filter tasks by status/priority
  • update-task - Update existing tasks
  • delete-task - Remove tasks by ID

Weather & System Tools

  • get-weather - Fetch weather data and forecasts
  • get-system-info - System metrics (CPU, memory, load)

Claude Desktop Configuration

To connect your MCP server to Claude Desktop, add this configuration to your Claude config file:

Claude Desktop MCP Configuration

typescript

1{
2  "mcpServers": {
3    "moro-mcp-server": {
4      "command": "node",
5      "args": ["path/to/your/moro-mcp-server/dist/server.js"],
6      "env": {
7        "NODE_ENV": "production"
8      }
9    }
10  }
11}

Development Mode

For development and debugging, you can run the server in HTTP mode:

npm run dev http

Run the Example

Getting Started

typescript

1# Clone and setup
2git clone https://github.com/Moro-JS/examples.git
3cd examples/mcp-server
4
5# Install dependencies
6npm install
7
8# Build the server
9npm run build
10
11# Test in HTTP mode (for debugging)
12npm run dev http
13# Server runs at http://localhost:3010
14
15# Test in MCP mode (for AI agents)
16npm run dev
17# Ready for Claude Desktop connection
18
19# Test with MCP Inspector
20npm run test:mcp
21
22# Available commands:
23npm run dev          # Start in MCP mode
24npm run dev http     # Start in HTTP debug mode  
25npm run build        # Build for production
26npm run start        # Production MCP mode
27npm run test:mcp     # Test with MCP Inspector

What You'll Learn

  • • Building MCP servers with enterprise architecture
  • • Integrating AI agents with your MoroJS applications
  • • Dual transport support (STDIO + HTTP)
  • • Modular design patterns for scalable MCP servers
  • • Type-safe tool definitions and validation
  • • Debugging and testing MCP integrations

Why MCP with MoroJS?

🚀 Rapid Development

MoroJS's modular architecture makes it easy to build and scale MCP servers with clean separation of concerns.

🔒 Enterprise Ready

Built-in security, validation, and error handling make your MCP servers production-ready from day one.

🔧 Dual Mode Support

Debug with HTTP endpoints during development, then switch to STDIO for AI agent integration.

📈 Future-Proof

Position your applications for the AI agent revolution with standardized MCP integration.

Next Steps