Examples
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
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 modules
9import TasksModule from './modules/tasks';
10import WeatherModule from './modules/weather';
11import SystemModule from './modules/system';
12import MCPCoreModule from './modules/mcp-core';
13
14// Import MCP handlers
15import { setupMCPHandlers } from './modules/mcp-core/handlers';
16
17async function createMCPServer() {
18 // Create MoroJS app with enterprise configuration
19 const app = await createApp({
20 cors: true,
21 compression: true,
22 helmet: true,
23 });
24
25 // Mock database for demonstration
26 const mockDatabase = {
27 tasks: [],
28 weather: {},
29 system: {},
30 };
31
32 // Register database and event system
33 app.database(mockDatabase);
34
35 // Enterprise middleware for logging
36 app.use((req: any, res: any, next: () => void) => {
37 console.error(`[${new Date().toISOString()}] ${req.method} ${req.path}`);
38 next();
39 });
40
41 // Load enterprise modules
42 await app.loadModule(TasksModule);
43 await app.loadModule(WeatherModule);
44 await app.loadModule(SystemModule);
45 await app.loadModule(MCPCoreModule);
46
47 // Root endpoint
48 app.get('/', (req, res) => {
49 return {
50 message: 'MoroJS MCP Server',
51 description: 'Model Context Protocol server built with enterprise MoroJS architecture',
52 version: '1.0.0',
53 architecture: 'modular-enterprise',
54 capabilities: ['tools', 'resources', 'prompts'],
55 modules: ['tasks', 'weather', 'system', 'mcp-core'],
56 endpoints: {
57 tasks: '/api/v1.0.0/tasks/',
58 weather: '/api/v1.0.0/weather/',
59 system: '/api/v1.0.0/system/',
60 },
61 modes: {
62 mcp: 'Default - Use with AI agents (Claude Desktop, etc.)',
63 http: 'Debug mode - Use "npm run dev http" for HTTP debugging',
64 },
65 };
66 });
67
68 return app;
69}1async function createMCPProtocolServer(context: { database: any; events: any }) {
2 // Create MCP protocol server
3 const mcpServer = new Server(
4 {
5 name: 'moro-mcp-server',
6 version: '1.0.0',
7 },
8 {
9 capabilities: {
10 resources: {},
11 tools: {},
12 prompts: {},
13 },
14 }
15 );
16
17 // Setup MCP handlers with module integration
18 setupMCPHandlers(mcpServer, context);
19
20 return mcpServer;
21}
22
23async function startMCPMode() {
24 console.error('Starting MoroJS MCP Server in MCP mode...');
25
26 // Create context
27 const context = {
28 database: { tasks: [], weather: {}, system: {} },
29 events: { emit: async (event: string, data: any) => console.error(`Event: ${event}`, data) }
30 };
31
32 // Create and start MCP server
33 const mcpServer = await createMCPProtocolServer(context);
34 const transport = new StdioServerTransport();
35 await mcpServer.connect(transport);
36
37 console.error('MCP Server connected via stdio transport');
38 console.error('Server ready for AI agent connections');
39}
40
41async function startHTTPMode(port: number = 3010) {
42 console.log('Starting MoroJS MCP Server in HTTP debug mode...');
43
44 const app = await createMCPServer();
45
46 app.listen(port, () => {
47 console.log(`HTTP Debug Server running on http://localhost:${port}`);
48 console.log('');
49 console.log('Available endpoints:');
50 console.log(` Root: http://localhost:${port}/`);
51 console.log(` Tasks: http://localhost:${port}/api/v1.0.0/tasks/`);
52 console.log(` Weather: http://localhost:${port}/api/v1.0.0/weather/`);
53 console.log(` System: http://localhost:${port}/api/v1.0.0/system/`);
54 console.log('');
55 console.log('Built with MoroJS Enterprise Architecture');
56 });
57}
58
59// Main execution
60async function main() {
61 const mode = process.argv[2] || 'mcp';
62
63 if (mode === 'http' || mode === 'debug') {
64 // HTTP mode for debugging
65 await startHTTPMode(3010);
66 } else {
67 // MCP mode (default) for AI agent integration
68 await startMCPMode();
69 }
70}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:
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 httpRun the Example
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 InspectorWhat 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
Expose your API to AI agents over MCP without writing a separate integration.