Skip to content

sourcefuse/loopback4-mcp

Repository files navigation

ARC By SourceFuse logo

Powered By LoopBack 4

Overview

This extension provides a plug-and-play integration between LoopBack4 applications and the 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 McpToolRegistry service maintains all discovered tool metadata,their handlers and execution context.

    A McpToolRegistryBootObserver ensures that registration happens only after the application has fully booted.

Installation

npm install loopback4-mcp

Then register the component inside your application.ts.

this.component(McpComponent);

Usage

Add the @mcpTool() decorator to any controller in your application.

@mcpTool({
  name: 'create-user',
  description: 'Creates a new user in the system',
  schema?: {
    email: z.string().email(),
    name: z.string(),
  },
})
async createUser(args: {email: string; name: string}) {
  return {message: `User ${args.name} created`};
}

Mcp Hook Usage Details

To use hooks with MCP tools, follow the provider-based approach: Step 1: Create a hook provider:

// src/providers/my-hook.provider.ts
export class MyHookProvider implements Provider<McpHookFunction> {
  constructor(@inject(LOGGER.LOGGER_INJECT) private logger: ILogger) {}
  value(): McpHookFunction {
    return async (context: McpHookContext) => {
      this.logger.info(`Hook executed for tool: ${context.toolName}`);
    };
  }
}

Step 2: Add binding key to McpHookBindings:

// src/keys.ts
export namespace McpHookBindings {
  export const MY_HOOK = BindingKey.create<McpHookFunction>('hooks.mcp.myHook');
}

Step 3: Bind provider in application.ts:

this.bind(McpHookBindings.MY_HOOK).toProvider(MyHookProvider);

Step 4: Use in decorator:

@mcpTool({
 name: 'my-tool',
 description: 'my-description'
 preHookBinding: McpHookBindings.MY_HOOK,
  postHookBinding: 'hooks.mcp.myOtherHook' // or string binding key
})

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published