|
| 1 | +<a href="https://sourcefuse.github.io/arc-docs/arc-api-docs" target="_blank"><img src="https://github.com/sourcefuse/loopback4-microservice-catalog/blob/master/docs/assets/logo-dark-bg.png?raw=true" alt="ARC By SourceFuse logo" title="ARC By SourceFuse" align="right" width="150" /></a> |
| 2 | +# [loopback4-mcp](https://github.com/sourcefuse/loopback4-mcp) |
| 3 | +<p align="left"> |
| 4 | +</a> |
| 5 | +<a href="https://loopback.io/" target="_blank"> |
| 6 | +<img alt="Powered By LoopBack 4" src="https://img.shields.io/badge/Powered%20by-LoopBack 4-brightgreen" /> |
| 7 | +</a> |
| 8 | +</p> |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +This extension provides a plug-and-play integration between LoopBack4 applications and the Model Context Protocol (MCP) specification. |
| 13 | + |
| 14 | +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. |
| 15 | +### Key Features |
| 16 | +- Automatic MCP Tool Discovery :- |
| 17 | + The extension scans your application at boot time and automatically registers all methods decorated with the custom @mcpTool() decorator. |
| 18 | + |
| 19 | + This allows you to define MCP tools anywhere in your LoopBack project without manually wiring metadata. |
| 20 | + |
| 21 | +- Lifecycle-managed Tool Registry :- |
| 22 | + A dedicated `McpToolRegistry` service maintains all discovered tool metadata,their handlers and execution context. |
| 23 | + |
| 24 | + A `McpToolRegistryBootObserver` ensures that registration happens only after the application has fully booted. |
| 25 | +## Installation |
| 26 | +```sh |
| 27 | +npm install loopback4-mcp |
| 28 | +``` |
| 29 | +Then register the component inside your `application.ts`. |
| 30 | +```ts |
| 31 | +this.component(McpComponent); |
| 32 | +``` |
| 33 | +## Usage |
| 34 | +Add the `@mcpTool()` decorator to any controller in your application. |
| 35 | +```ts |
| 36 | +@mcpTool({ |
| 37 | + name: 'create-user', |
| 38 | + description: 'Creates a new user in the system', |
| 39 | + schema?: { |
| 40 | + email: z.string().email(), |
| 41 | + name: z.string(), |
| 42 | + }, |
| 43 | +}) |
| 44 | +async createUser(args: {email: string; name: string}) { |
| 45 | + return {message: `User ${args.name} created`}; |
| 46 | +} |
| 47 | +``` |
| 48 | +### Mcp Hook Usage Details |
| 49 | + To use hooks with MCP tools, follow the provider-based approach: |
| 50 | + Step 1: Create a hook provider: |
| 51 | + ```typescript |
| 52 | + // src/providers/my-hook.provider.ts |
| 53 | + export class MyHookProvider implements Provider<McpHookFunction> { |
| 54 | + constructor(@inject(LOGGER.LOGGER_INJECT) private logger: ILogger) {} |
| 55 | + value(): McpHookFunction { |
| 56 | + return async (context: McpHookContext) => { |
| 57 | + this.logger.info(`Hook executed for tool: ${context.toolName}`); |
| 58 | + }; |
| 59 | + } |
| 60 | + } |
| 61 | + ``` |
| 62 | + Step 2: Add binding key to McpHookBindings: |
| 63 | + ```typescript |
| 64 | + // src/keys.ts |
| 65 | + export namespace McpHookBindings { |
| 66 | + export const MY_HOOK = BindingKey.create<McpHookFunction>('hooks.mcp.myHook'); |
| 67 | + } |
| 68 | + ``` |
| 69 | + Step 3: Bind provider in `application.ts`: |
| 70 | + ```typescript |
| 71 | + this.bind(McpHookBindings.MY_HOOK).toProvider(MyHookProvider); |
| 72 | + ``` |
| 73 | + Step 4: Use in decorator: |
| 74 | + ```typescript |
| 75 | + @mcpTool({ |
| 76 | + name: 'my-tool', |
| 77 | + description: 'my-description' |
| 78 | + preHookBinding: McpHookBindings.MY_HOOK, |
| 79 | + postHookBinding: 'hooks.mcp.myOtherHook' // or string binding key |
| 80 | + }) |
0 commit comments