Skip to content
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
8 changes: 8 additions & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,14 @@
# - Standby owner(s): @samarpanB
/extensions/sequelize @shubhamp-sf @samarpanB

#
# MCP
#
# - Issue label: mcp
# - Primary owner(s): @akshatdubeysf
# - Standby owner(s): @samarpanB
/extensions/mcp @akshatdubeysf @samarpanB

#
# MessagePack body parser
#
Expand Down
2 changes: 2 additions & 0 deletions docs/site/Component.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ These components add additional capabilities to LoopBack.
Adds support for TypeORM in LoopBack
- [@loopback/sequelize](https://github.com/loopbackio/loopback-next/tree/master/extensions/sequelize) -
Adds support for Sequelize in LoopBack
- [@loopback/mcp](https://github.com/loopbackio/loopback-next/tree/master/extensions/mcp) -
Adds support to expose any controller as an MCP server

### Community extensions

Expand Down
11 changes: 11 additions & 0 deletions docs/site/MCP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
lang: en
title: 'Model Context Protocol (MCP)'
keywords: LoopBack 4.0, LoopBack 4, Node.js, TypeScript, OpenAPI, MCP
layout: readme
source: loopback-next
file: extensions/mcp/README.md
sidebar: lb4_sidebar
permalink: /doc/en/lb4/MCP.html
summary: Use `@loopback/mcp` to expose LoopBack APIs as MCP tools
---
4 changes: 3 additions & 1 deletion docs/site/MONOREPO.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ one in the monorepo: `npm run update-monorepo-file`
| [extensions/pooling](https://github.com/loopbackio/loopback-next/tree/master/extensions/pooling) | @loopback/pooling | Resource pooling service for LoopBack 4 |
| [extensions/socketio](https://github.com/loopbackio/loopback-next/tree/master/extensions/socketio) | @loopback/socketio | LoopBack's WebSocket server based on socket.io |
| [extensions/typeorm](https://github.com/loopbackio/loopback-next/tree/master/extensions/typeorm) | @loopback/typeorm | Adds support for TypeORM in LoopBack |
| [extensions/sequelize](https://github.com/loopbackio/loopback-next/tree/master/extensions/sequelize) | @loopback/sequelize | An extension that adds support for Sequelize to existing repositories. |
| [extensions/sequelize](https://github.com/loopbackio/loopback-next/tree/master/extensions/sequelize) | @loopback/sequelize | An extension that adds support for Sequelize to existing repositories.

| [extensions/mcp](https://github.com/loopbackio/loopback-next/tree/master/extensions/mcp) | @loopback/mcp | An extension that adds support to expose a controller as an MCP server |
| [fixtures/mock-oauth2-provider](https://github.com/loopbackio/loopback-next/tree/master/fixtures/mock-oauth2-provider) | @loopback/mock-oauth2-provider | An internal application to mock the OAuth2 authorization flow login with a social app like facebook, google etc |
| [fixtures/tsdocs-monorepo](https://github.com/loopbackio/loopback-next/tree/master/fixtures/tsdocs-monorepo) | _(private)_ | A monorepo for tsdocs testing |
| [packages/authentication](https://github.com/loopbackio/loopback-next/tree/master/packages/authentication) | @loopback/authentication | A LoopBack component for authentication support. |
Expand Down
4 changes: 4 additions & 0 deletions docs/site/sidebars/lb4_sidebar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,10 @@ children:
url: Pooling.html
output: 'web, pdf'

- title: 'Model Context Protocol (MCP)'
url: MCP.html
output: 'web, pdf'

- title: 'Troubleshooting'
url: Troubleshooting.html
output: 'web, pdf'
Expand Down
2 changes: 2 additions & 0 deletions extensions/mcp/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package-lock=true
scripts-prepend-node-path=true
25 changes: 25 additions & 0 deletions extensions/mcp/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright (c) LoopBack contributors 2026.
Node module: @loopback/mcp
This project is licensed under the MIT License, full text below.

---

MIT license

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
135 changes: 135 additions & 0 deletions extensions/mcp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<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>

# [loopback4-mcp](https://github.com/sourcefuse/loopback4-mcp)

<p align="left">
<a href="https://www.npmjs.com/package/loopback4-mcp">
<img src="https://img.shields.io/npm/v/loopback4-mcp.svg" alt="npm version" />
</a>
<a href="https://sonarcloud.io/summary/new_code?id=sourcefuse_loopback4-mcp" target="_blank">
<img alt="Sonar Quality Gate" src="https://img.shields.io/sonar/quality_gate/sourcefuse_loopback4-mcp?server=https%3A%2F%2Fsonarcloud.io">
</a>
<a href="https://github.com/sourcefuse/loopback4-mcp/graphs/contributors" target="_blank">
<img alt="GitHub contributors" src="https://img.shields.io/github/contributors/sourcefuse/loopback4-mcp?">
</a>
<a href="https://www.npmjs.com/package/loopback4-mcp" target="_blank">
<img alt="downloads" src="https://img.shields.io/npm/dw/loopback4-mcp.svg">
</a>
<a href="./LICENSE">
<img src="https://img.shields.io/github/license/sourcefuse/loopback4-mcp.svg" alt="License" />
</a>
<a href="https://loopback.io/" target="_blank">
<img alt="Powered By LoopBack 4" src="https://img.shields.io/badge/Powered%20by-LoopBack 4-brightgreen" />
</a>
</p>

## 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

```sh
npm install @loopback/mcp
```

## Basic Usage

Configure and load `McpComponent` in the application constructor
as shown below.

```ts
import {McpComponent} from 'loopback4-mcp';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change the naming here


export class MyApplication extends BootMixin(
ServiceMixin(RepositoryMixin(RestApplication)),
) {
constructor(options: ApplicationConfig = {}) {
super();
this.component(McpComponent);
}
}
```

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

All MCP tool methods must use LoopBack `@param` decorators to define their input parameters.If `@param` decorators are missing, the MCP tool will fail.

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

This decorator accepts a total of five fields, out of which `name` and `description` are mandatory and `schema`,`preHook` and `postHook` are optional enhancements.

The schema field allows defining a Zod-based validation schema for tool input parameters, while preHook and postHook enable execution of custom logic before and after the tool handler runs.

## Mcp Hook Usage Details

To use hooks with MCP tools, follow the provider-based approach:

Step 1: Create a hook provider:

```ts
// 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:

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

Step 3: Bind provider in `application.ts`:

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

Step 4: Use in decorator:

```ts
@mcpTool({
name: 'my-tool',
description: 'my-description'
preHookBinding: McpHookBindings.MY_HOOK,
postHookBinding: 'hooks.mcp.myOtherHook' // or string binding key
})
```
146 changes: 146 additions & 0 deletions extensions/mcp/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
{
"name": "@loopback/mcp",
"version": "0.0.1",
"private": false,
"description": "A loopback extension that exposes API to MCP Tools.",
"keywords": [
"loopback-extension",
"loopback",
"loopback4",
"mcp",
"mcp-tool"
],
"main": "dist/index.js",
"types": "dist/index.d.ts",
"engines": {
"node": ">=20"
},
"scripts": {
"build": "lb-tsc",
"build:watch": "lb-tsc --watch",
"lint": "npm run eslint && npm run prettier:check",
"lint:fix": "npm run eslint:fix && npm run prettier:fix",
"prettier:cli": "lb-prettier \"**/*.ts\" \"**/*.js\"",
"prettier:check": "npm run prettier:cli -- -l",
"prettier:fix": "npm run prettier:cli -- --write",
"eslint": "lb-eslint --report-unused-disable-directives .",
"eslint:fix": "npm run eslint -- --fix",
"pretest": "npm run rebuild",
"test": "lb-mocha --allow-console-logs \"dist/__tests__\"",
"posttest": "npm run lint",
"test:dev": "lb-mocha --allow-console-logs dist/__tests__/**/*.js && npm run posttest",
"clean": "lb-clean dist *.tsbuildinfo .eslintcache",
"rebuild": "npm run clean && npm run build",
"prepublishOnly": "npm run test"
},
"repository": {
"type": "git",
"url": "https://github.com/sourcefuse/loopback4-mcp"
},
"author": "SourceFuse",
"license": "MIT",
"files": [
"README.md",
"dist",
"src",
"!*/__tests__"
],
"peerDependencies": {
"@loopback/core": "^7.0.3"
},
"dependencies": {
"@loopback/rest": "^15.0.7",
"@modelcontextprotocol/sdk": "^1.24.0",
"@sourceloop/core": "^20.0.0",
"tslib": "^2.6.2",
"zod": "^4.2.1"
},
"devDependencies": {
"@commitlint/cli": "^17.7.1",
"@commitlint/config-conventional": "^17.7.0",
"@loopback/boot": "^8.0.11",
"@loopback/build": "^12.0.3",
"@loopback/core": "^7.0.3",
"@loopback/eslint-config": "^16.0.1",
"@loopback/repository": "^8.0.10",
"@loopback/rest-explorer": "^8.0.11",
"@loopback/service-proxy": "^8.0.10",
"@loopback/testlab": "^8.0.3",
"@semantic-release/changelog": "^6.0.1",
"@semantic-release/commit-analyzer": "^9.0.2",
"@semantic-release/git": "^10.0.1",
"@semantic-release/npm": "^13.1.3",
"@semantic-release/release-notes-generator": "^10.0.3",
"@types/jsonwebtoken": "^9.0.10",
"@types/node": "^16.18.126",
"commitizen": "^4.2.4",
"cz-conventional-changelog": "^3.3.0",
"cz-customizable": "^6.3.0",
"cz-customizable-ghooks": "^2.0.0",
"eslint": "^8.57.1",
"husky": "^7.0.4",
"loopback4-authentication": "^13.0.4",
"loopback4-authorization": "^8.1.3",
"semantic-release": "^25.0.2",
"source-map-support": "^0.5.21",
"typescript": "~5.2.2"
},
"overrides": {
"jws": "3.2.3",
"node-forge": "1.3.2",
"qs": "6.14.1"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-customizable"
},
"cz-customizable": {
"config": "./.cz-config.js"
}
},
"publishConfig": {
"registry": "https://registry.npmjs.org/",
"access": "public"
},
"release": {
"branches": [
"master"
],
"plugins": [
[
"@semantic-release/commit-analyzer",
{
"preset": "angular",
"releaseRules": [
{
"type": "chore",
"scope": "deps",
"release": "patch"
}
]
}
],
"@semantic-release/release-notes-generator",
[
"@semantic-release/npm",
{
"npmPublish": true,
"pkgRoot": ".",
"tarballDir": "dist"
}
],
[
"@semantic-release/git",
{
"assets": [
"package.json",
"CHANGELOG.md"
],
"message": "chore(release): ${nextRelease.version} semantic"
}
],
"@semantic-release/github"
],
"repositoryUrl": "https://github.com/sourcefuse/loopback4-mcp.git"
}
}
Loading
Loading