Skip to content

chore: mcp #3929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions apps/mcp-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# MCP Example with Module Federation

This example demonstrates how to build and host MCP (Model Context Protocol) servers using Module Federation.

## Structure

- **host/**: The main MCP host that loads and aggregates MCP servers
- **remote1/**: MCP servers for filesystem and tools
- **remote2/**: MCP servers for git and database operations

## Features

1. **Dynamic Loading**: MCP servers are loaded dynamically via Module Federation
2. **Tool Namespacing**: All tools are namespaced by server name to avoid conflicts
3. **Registry Pattern**: Centralized registry manages all loaded MCP servers
4. **Unified Interface**: Single host server aggregates all tools from multiple servers

## Architecture

```
┌─────────────────┐
│ MCP Host │
│ │
│ ┌───────────┐ │
│ │ Registry │ │ ◄── Manages all MCP servers
│ └───────────┘ │
│ │
│ ┌───────────┐ │
│ │Federation │ │ ◄── Loads remote servers
│ │ Loader │ │
│ └───────────┘ │
└────────┬────────┘
┌────┴────┐
│ │
┌────▼──┐ ┌──▼─────┐
│Remote1│ │Remote2 │
│ │ │ │
│ - fs │ │ - git │
│ - tools │ - db │
└───────┘ └────────┘
```

## Running the Example

1. Install dependencies:
```bash
pnpm install
```

2. Build all apps:
```bash
# Build remotes first
nx build mcp-example-remote1
nx build mcp-example-remote2

# Then build the host
nx build mcp-example-host
```

3. Start the remote servers:
```bash
# Terminal 1
nx serve mcp-example-remote1

# Terminal 2
nx serve mcp-example-remote2
```

4. Run the host:
```bash
node apps/mcp-example/host/dist/main.js
```

## Available Tools

### Remote 1 - Filesystem & Tools
- `filesystem.read_file` - Read file contents
- `filesystem.write_file` - Write file contents
- `filesystem.list_directory` - List directory contents
- `tools.process_info` - Get process information
- `tools.system_info` - Get system information

### Remote 2 - Git & Database
- `git.git_status` - Get git repository status
- `git.git_log` - Get git commit history
- `database.db_get` - Get value from database
- `database.db_set` - Set value in database
- `database.db_list` - List all database keys

## Module Federation Configuration

The host loads MCP servers using Module Federation:

```javascript
remotes: {
mcp_remote1: 'mcp_remote1@http://localhost:3030/remoteEntry.js',
mcp_remote2: 'mcp_remote2@http://localhost:3031/remoteEntry.js',
}
```

Each remote exposes its MCP servers:

```javascript
exposes: {
'./filesystem': './src/filesystem-server.ts',
'./tools': './src/tools-server.ts',
}
```

## Creating Your Own MCP Server

1. Create a new server implementing the MCP protocol
2. Export a factory function that returns the server instance
3. Add it to a remote's exposes configuration
4. Update the federation loader to load your server

Example:
```typescript
export function createMyServer() {
const server = new Server({
name: 'my-server',
version: '1.0.0',
});

// Add handlers...

return server;
}
```

## Benefits

1. **Modularity**: Each MCP server can be developed and deployed independently
2. **Dynamic Loading**: Load only the servers you need at runtime
3. **Version Management**: Module Federation handles versioning and updates
4. **Tool Isolation**: Namespacing prevents conflicts between servers
5. **Scalability**: Easy to add new MCP servers without modifying the host
10 changes: 10 additions & 0 deletions apps/mcp-example/host/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "mcp-host",
"version": "0.0.1",
"dependencies": {
"@modelcontextprotocol/sdk": "^0.5.0",
"@module-federation/enhanced": "workspace:*",
"@module-federation/node": "workspace:*"
},
"type": "commonjs"
}
78 changes: 78 additions & 0 deletions apps/mcp-example/host/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"name": "mcp-example-host",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "apps/mcp-example/host/src",
"projectType": "application",
"tags": ["mcp", "host"],
"targets": {
"build": {
"executor": "@nx/webpack:webpack",
"outputs": ["{options.outputPath}"],
"defaultConfiguration": "production",
"options": {
"target": "node",
"compiler": "tsc",
"outputPath": "apps/mcp-example/host/dist",
"main": "apps/mcp-example/host/src/main.ts",
"tsConfig": "apps/mcp-example/host/tsconfig.app.json",
"webpackConfig": "apps/mcp-example/host/webpack.config.js"
},
"configurations": {
"development": {},
"production": {}
},
"dependsOn": [
{
"target": "build",
"dependencies": true
}
]
},
"serve": {
"executor": "@nx/js:node",
"defaultConfiguration": "development",
"dependsOn": [
{
"target": "build",
"dependencies": true
}
],
"options": {
"buildTarget": [
"mcp-example-host:build",
"mcp-example-remote1:build",
"mcp-example-remote2:build"
]
},
"configurations": {
"development": {
"buildTarget": "mcp-example-host:build:development"
},
"production": {
"buildTarget": "mcp-example-host:build:production"
}
}
},
"lint": {
"executor": "@nx/eslint:lint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["apps/mcp-example/host/**/*.ts"]
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "apps/mcp-example/host/jest.config.ts",
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
}
}
}
}
47 changes: 47 additions & 0 deletions apps/mcp-example/host/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { RemoteConfig } from './types';

// Production configuration
export const PRODUCTION_CONFIG: RemoteConfig[] = [
{
name: 'mcp_remote1',
url: 'http://localhost:3030/remoteEntry.js',
modules: {
filesystem: '/filesystem',
tools: '/tools',
},
},
{
name: 'mcp_remote2',
url: 'http://localhost:3031/remoteEntry.js',
modules: {
git: '/git',
database: '/database',
},
},
];

// Development configuration with local paths
export const DEVELOPMENT_CONFIG: RemoteConfig[] = [
{
name: 'mcp_remote1',
url: 'http://localhost:3030/remoteEntry.js',
modules: {
filesystem: '/filesystem',
tools: '/tools',
},
},
{
name: 'mcp_remote2',
url: 'http://localhost:3031/remoteEntry.js',
modules: {
git: '/git',
database: '/database',
},
},
];

// Get configuration based on environment
export function getConfig(): RemoteConfig[] {
const env = process.env.NODE_ENV || 'development';
return env === 'production' ? PRODUCTION_CONFIG : DEVELOPMENT_CONFIG;
}
Loading