Skip to content

Commit 3ff863a

Browse files
docs(maintenance): readme updated
readme updated GH-5
1 parent c5d2528 commit 3ff863a

File tree

2 files changed

+73
-39
lines changed

2 files changed

+73
-39
lines changed

README.md

Lines changed: 71 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
This extension provides a plug-and-play integration between LoopBack4 applications and the Model Context Protocol (MCP) specification.
2929

3030
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.
31+
3132
### Key Features
33+
3234
- Automatic MCP Tool Discovery :-
3335
The extension scans your application at boot time and automatically registers all methods decorated with the custom @mcpTool() decorator.
3436

@@ -38,16 +40,35 @@ Its purpose is to enable LoopBack APIs, services, and business logic to be expos
3840
A dedicated `McpToolRegistry` service maintains all discovered tool metadata,their handlers and execution context.
3941

4042
A `McpToolRegistryBootObserver` ensures that registration happens only after the application has fully booted.
43+
4144
## Installation
45+
4246
```sh
4347
npm install loopback4-mcp
4448
```
45-
Then register the component inside your `application.ts`.
49+
50+
## Basic Usage
51+
52+
Configure and load `McpComponent` in the application constructor
53+
as shown below.
54+
4655
```ts
47-
this.component(McpComponent);
56+
import {McpComponent} from 'loopback4-mcp';
57+
58+
export class MyApplication extends BootMixin(
59+
ServiceMixin(RepositoryMixin(RestApplication)),
60+
) {
61+
constructor(options: ApplicationConfig = {}) {
62+
super();
63+
this.component(McpComponent);
64+
}
65+
}
4866
```
49-
## Usage
67+
5068
Add the `@mcpTool()` decorator to any controller in your application.
69+
70+
All MCP tool methods must use LoopBack `@param` decorators to define their input parameters.If `@param` decorators are missing, the MCP tool will fail.
71+
5172
```ts
5273
@mcpTool({
5374
name: 'create-user',
@@ -57,47 +78,58 @@ Add the `@mcpTool()` decorator to any controller in your application.
5778
name: z.string(),
5879
},
5980
})
60-
async createUser(args: {email: string; name: string}) {
61-
return {message: `User ${args.name} created`};
81+
async createUser(
82+
@param.query.string('email') email: string,
83+
@param.query.string('name') name: string,
84+
) {
85+
return {message: `User ${name} created`};
6286
}
6387
```
6488

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

6791
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.
6892

69-
### Mcp Hook Usage Details
70-
To use hooks with MCP tools, follow the provider-based approach:
71-
72-
Step 1: Create a hook provider:
73-
```ts
74-
// src/providers/my-hook.provider.ts
75-
export class MyHookProvider implements Provider<McpHookFunction> {
76-
constructor(@inject(LOGGER.LOGGER_INJECT) private logger: ILogger) {}
77-
value(): McpHookFunction {
78-
return async (context: McpHookContext) => {
79-
this.logger.info(`Hook executed for tool: ${context.toolName}`);
80-
};
81-
}
82-
}
83-
```
84-
Step 2: Add binding key to McpHookBindings:
85-
```ts
86-
// src/keys.ts
87-
export namespace McpHookBindings {
88-
export const MY_HOOK = BindingKey.create<McpHookFunction>('hooks.mcp.myHook');
93+
## Mcp Hook Usage Details
94+
95+
To use hooks with MCP tools, follow the provider-based approach:
96+
97+
Step 1: Create a hook provider:
98+
99+
```ts
100+
// src/providers/my-hook.provider.ts
101+
export class MyHookProvider implements Provider<McpHookFunction> {
102+
constructor(@inject(LOGGER.LOGGER_INJECT) private logger: ILogger) {}
103+
value(): McpHookFunction {
104+
return async (context: McpHookContext) => {
105+
this.logger.info(`Hook executed for tool: ${context.toolName}`);
106+
};
89107
}
90-
```
91-
Step 3: Bind provider in `application.ts`:
92-
```typescript
93-
this.bind(McpHookBindings.MY_HOOK).toProvider(MyHookProvider);
94-
```
95-
Step 4: Use in decorator:
96-
```ts
97-
@mcpTool({
98-
name: 'my-tool',
99-
description: 'my-description'
100-
preHookBinding: McpHookBindings.MY_HOOK,
101-
postHookBinding: 'hooks.mcp.myOtherHook' // or string binding key
102-
})
103-
```
108+
}
109+
```
110+
111+
Step 2: Add binding key to McpHookBindings:
112+
113+
```ts
114+
// src/keys.ts
115+
export namespace McpHookBindings {
116+
export const MY_HOOK = BindingKey.create<McpHookFunction>('hooks.mcp.myHook');
117+
}
118+
```
119+
120+
Step 3: Bind provider in `application.ts`:
121+
122+
```typescript
123+
this.bind(McpHookBindings.MY_HOOK).toProvider(MyHookProvider);
124+
```
125+
126+
Step 4: Use in decorator:
127+
128+
```ts
129+
@mcpTool({
130+
name: 'my-tool',
131+
description: 'my-description'
132+
preHookBinding: McpHookBindings.MY_HOOK,
133+
postHookBinding: 'hooks.mcp.myOtherHook' // or string binding key
134+
})
135+
```

src/services/mcp-tool-registry.service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export class McpToolRegistry {
5151
const processedTools: McpToolMetadata[] = [];
5252
const prototype = targetClass.prototype;
5353

54+
// Retrieve all tool metadata that was stored earlier by @mcpTool decorator
5455
const allTools = MetadataInspector.getAllMethodMetadata<McpToolMetadata>(
5556
MCP_TOOL_METADATA_KEY,
5657
prototype,
@@ -116,6 +117,7 @@ export class McpToolRegistry {
116117
return [];
117118
}
118119

120+
// extracts all methods decroated with @mcpTool
119121
const tools = this.getMcpToolsFromClass(controllerClass);
120122

121123
if (tools.length === 0) return [];

0 commit comments

Comments
 (0)