loopback4-mcp¶
Overview¶
This extension provides a plug-and-play integration between LoopBack4 applications and Model Context Protocol (MCP) specification. Its purpose is to enable LoopBack APIs, services, and business logic to be exposed as MCP Tools, allowing external MCP clients (such as LLMs, agents, or MCP-compatible apps) to discover and execute server-defined operations.
Key Features¶
- Automatic MCP Tool Discovery: The extension scans your application at boot time and automatically registers all methods decorated with the custom
@mcpTool()decorator. This allows you to define MCP tools anywhere in your LoopBack project without manually wiring metadata.
- Lifecycle-managed Tool Registry: A dedicated
McpToolRegistryservice maintains all discovered tool metadata, their handlers, and execution context. AMcpToolRegistryBootObserverensures that registration happens only after the application has fully booted.
- Hook System Support: Built-in support for pre and post hooks that enable validation, logging, audit trails, and custom business logic around tool execution.
- Authorization Integration: Seamless integration with LoopBack's authorization system, ensuring MCP tools respect your existing permission structure.
- Simplified Endpoint Format: Easy-to-use POST endpoint that accepts tool arguments directly without complex MCP protocol wrapping.
Installation¶
Integration Steps¶
Step 1: Create Binding Keys¶
Create a src/keys.ts file to define binding keys for MCP hooks:
Step 2: Create Hook Providers¶
Create hook providers to implement pre and post-hook functionality:
Step 3: Configure Application¶
Update your src/application.ts to bind the component and hooks:
Step 4: Create MCP Tool Controllers¶
Add the @mcpTool() decorator to controller methods you want to expose as MCP tools. Here's a complete example showing the decorator stack with authorization and authentication:
Parameter Decorator Guidelines¶
Critical: Use correct LoopBack @param decorators based on your route structure.
Path Parameters¶
Query Parameters¶
Request Body Parameters¶
Common Mistakes:
- ❌ Using @param.query.string('id') for /users/{id} routes
- ✅ Use @param.path.string('id') for path parameters
Decorator Configuration¶
The @mcpTool() decorator accepts the following configuration:
Testing¶
Using MCP Inspector¶
The easiest way to test and visualize your MCP tools is using the MCP Inspector. This provides a web interface where all your endpoints are available and you can see hook responses in real-time.
Start MCP Inspector:
What MCP Inspector shows: - All Available Tools: Complete list of MCP tools exposed by your application - Tool Details: Names, descriptions, and parameter schemas - Live Testing: Test each tool directly from the web interface - Request/Response: Real-time request and response data - Hook Execution: Visualize pre and post-hook execution and their responses - Error Handling: See error messages and debugging information
Workflow with MCP Inspector: 1. Start your LoopBack application 2. Run MCP Inspector with your MCP endpoint URL 3. Browse available tools in the web interface 4. Test tools with different parameters 5. Monitor hook execution and responses 6. Debug issues using the detailed logs
License¶
Support¶
- GitHub Issues: sourcefuse/loopback4-mcp/issues
- Documentation: loopback.io
