Aller au contenu principal

MCP Integration Summary

Overview

The Model Context Protocol (MCP) server has been successfully integrated into the Baldr API. This allows Large Language Models (LLMs) like Claude to interact with your API through a standardized protocol.

Files Created

Core Implementation

  1. src/services/mcp.service.ts - Core MCP server service

    • Manages tools, resources, and prompts
    • Handles MCP protocol requests
    • Provides registration methods for capabilities
  2. src/mcp-server.ts - Standalone MCP server entry point

    • Connects to database
    • Initializes MCP capabilities
    • Runs as a separate process
  3. src/types/mcp.types.ts - TypeScript types for MCP

    • Tool definitions
    • Resource definitions
    • Prompt definitions

Configuration

  1. src/configs/mcp.config.ts - Basic MCP tools configuration

    • API health check
    • Module listing
    • Collection querying
    • Statistics retrieval
  2. src/configs/mcp.advanced.config.ts - Advanced examples

    • User management tools
    • Product search and analytics
    • Order management
    • Dashboard statistics
    • Module toggling

HTTP Interface

  1. src/controllers/mcp.controller.ts - HTTP endpoints for testing

    • Status endpoint: GET /api/mcp/status
    • Info endpoint: GET /api/mcp/info
    • Tools endpoint: GET /api/mcp/tools
  2. src/routes/mcp.route.ts - Route registration

Testing & Documentation

  1. src/scripts/test-mcp.ts - Test client for MCP server
  2. documents/MCP_INTEGRATION.md - Comprehensive documentation
  3. MCP_QUICKSTART.md - Quick start guide
  4. mcp-client-config.example.json - Example client configuration

Package Dependencies

Added:

  • @modelcontextprotocol/sdk - Official MCP SDK

NPM Scripts Added

{
"mcp": "node dist/mcp-server.js",
"mcp:dev": "nodemon src/mcp-server.ts",
"mcp:test": "ts-node src/scripts/test-mcp.ts"
}

How to Use

1. Start the MCP Server

# Development
npm run mcp:dev

# Production
npm run build && npm run mcp

2. Test the Connection

npm run mcp:test

3. Configure Claude Desktop

Edit your Claude Desktop config file and add:

{
"mcpServers": {
"baldr-api": {
"command": "node",
"args": ["/absolute/path/to/BaldrTs/dist/mcp-server.js"],
"env": {
"NODE_ENV": "production"
}
}
}
}

4. Test via HTTP (optional)

curl http://localhost:3000/api/mcp/status

Default Tools Available

  1. get_api_health - Check API health status
  2. list_modules - List available modules
  3. query_collection - Query MongoDB collections
  4. get_api_stats - Get API usage statistics

Architecture

┌─────────────────┐
│ LLM (Claude) │
└────────┬────────┘
│ MCP Protocol (stdio)

┌────────▼────────┐
│ MCP Server │◄─── src/mcp-server.ts
│ (Standalone) │
└────────┬────────┘

┌────▼─────────────────────┐
│ MCP Service │◄─── src/services/mcp.service.ts
│ - Tools Registry │
│ - Resources Registry │
│ - Prompts Registry │
└────┬─────────────────────┘

┌────▼─────────────────────┐
│ Tool Implementations │◄─── src/configs/mcp.config.ts
│ - API Operations │
│ - Database Queries │
│ - Business Logic │
└──────────────────────────┘

┌────▼─────────────────────┐
│ Baldr API │
│ - Models │
│ - Services │
│ - Database │
└──────────────────────────┘

Protocol Communication

The MCP server uses stdio transport, meaning it communicates via stdin/stdout. This is:

  • ✅ Secure for local/trusted environments
  • ✅ Easy to integrate with desktop applications
  • ✅ Standard for MCP implementations
  • ✅ Supported by Claude Desktop natively

Optional HTTP Interface

For testing and debugging, HTTP endpoints are available:

  • GET /api/mcp/status - Check server status
  • GET /api/mcp/info - Get server capabilities
  • GET /api/mcp/tools - List available tools

Next Steps

For Basic Usage

  1. Start the MCP server
  2. Configure your MCP client (e.g., Claude Desktop)
  3. Test the connection
  4. Start using tools in conversations

For Advanced Usage

  1. Review src/configs/mcp.advanced.config.ts
  2. Implement real database operations in tools
  3. Add authentication/authorization
  4. Create custom tools for your specific use cases
  5. Add more resources and prompts

For Production

  1. Secure the MCP server appropriately
  2. Add rate limiting if needed
  3. Implement comprehensive logging
  4. Monitor tool usage
  5. Set up error tracking

Security Considerations

  • The MCP server runs as a separate process
  • It connects to the same database as your main API
  • Tools should validate inputs and check permissions
  • Consider adding authentication to tool handlers
  • HTTP endpoints can use existing auth middleware
  • Stdio transport is secure for local/trusted environments

Customization Examples

Adding a New Tool

// In src/configs/mcp.config.ts
mcpService.registerTool(
"create_user",
"Create a new user account",
{
type: "object",
properties: {
email: { type: "string" },
name: { type: "string" },
},
required: ["email", "name"],
},
async (args) => {
// Implementation
return { success: true, userId: "..." };
}
);

Adding a Resource

mcpService.registerResource(
"baldr://api/schema",
"API Schema",
"OpenAPI schema for all endpoints",
"application/json"
);

Adding a Prompt

mcpService.registerPrompt(
"optimize_query",
"Optimize a database query",
[
{ name: "collection", required: true },
{ name: "filter", required: false },
]
);

Documentation Files

  • MCP_QUICKSTART.md - Quick start guide (start here!)
  • documents/MCP_INTEGRATION.md - Comprehensive documentation
  • mcp-client-config.example.json - Example client config
  • src/configs/mcp.advanced.config.ts - Advanced examples

Testing

Run the test client:

npm run mcp:test

This will:

  • Connect to the MCP server
  • List available tools
  • Test a tool call
  • List resources and prompts
  • Report results

Troubleshooting

Check the documentation files for detailed troubleshooting guides:

  • Connection issues
  • Configuration problems
  • Tool execution errors
  • Build failures

Support


Status: ✅ Fully integrated and tested Build: ✅ All files compile successfully Ready: ✅ Ready for use with MCP clients