Skip to content

Commit 258b02c

Browse files
authored
Merge pull request #2 from ribeirogab/update-docs
feat: add class-based tool implementation for MCP server
2 parents 98daa56 + fe12846 commit 258b02c

File tree

6 files changed

+91
-87
lines changed

6 files changed

+91
-87
lines changed

README.md

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { z } from 'zod';
2424
// Create a server instance
2525
const server = new McpServer({ name: 'my-server' });
2626

27-
// Add a tool
27+
// Register the tool with the server
2828
server.tool({
2929
name: 'greet',
3030
parameters: {
@@ -35,7 +35,7 @@ server.tool({
3535
content: [
3636
{
3737
type: 'text',
38-
text: `Hello, ${name}!`
38+
text: `Hello, ${name}! Nice to meet you.`
3939
}
4040
]
4141
};
@@ -46,6 +46,47 @@ server.tool({
4646
server.start({ transportType: 'stdio' });
4747
```
4848

49+
## Class-based Implementation
50+
51+
You can also implement MCP tools using classes:
52+
53+
```typescript
54+
import { McpServer, type McpTool } from 'simple-mcp';
55+
import { z } from 'zod';
56+
57+
const parameters = z.object({
58+
name: z.string().describe('The name is required'),
59+
});
60+
61+
class GreetTool implements McpTool<typeof parameters.shape> {
62+
public readonly name = 'greet';
63+
public readonly parameters = parameters.shape;
64+
65+
public async execute({ name }: z.infer<typeof parameters>) {
66+
return {
67+
content: [
68+
{
69+
type: 'text',
70+
text: `Hello, ${name}! Nice to meet you.`,
71+
},
72+
],
73+
};
74+
}
75+
}
76+
77+
// Initialize a new MCP server with the name 'greet-server'
78+
const server = new McpServer({ name: 'greet-server' });
79+
80+
// Create an instance of the GreetTool class
81+
const greetTool = new GreetTool();
82+
83+
// Register the tool with the server
84+
server.tool(greetTool);
85+
86+
// Start the server using stdio as the transport method
87+
server.start({ transportType: 'stdio' });
88+
```
89+
4990
## Examples
5091

5192
Check out the [examples directory](https://github.com/ribeirogab/simple-mcp/tree/main/examples) for more complete examples:

examples/README.md

Lines changed: 0 additions & 81 deletions
This file was deleted.

examples/calculator.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
import { McpServer, type McpTool } from 'simple-mcp';
12
import { z } from 'zod';
23

3-
import { McpServer, type McpTool } from '../dist';
4-
54
// Initialize a new MCP server with the name 'calculator-server'
65
const server = new McpServer({ name: 'calculator-server', version: '1.0.0' });
76

examples/greet-class.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { McpServer, type McpTool } from 'simple-mcp';
2+
import { z } from 'zod';
3+
4+
const parameters = z.object({
5+
name: z.string().describe('The name is required'),
6+
});
7+
8+
/**
9+
* GreetTool class implements the McpTool interface
10+
* This demonstrates how to create an MCP tool using a class-based approach
11+
*/
12+
class GreetTool implements McpTool<typeof parameters.shape> {
13+
// Tool name
14+
public readonly name = 'greet';
15+
16+
// Define parameters with Zod schema
17+
public readonly parameters = parameters.shape;
18+
19+
/**
20+
* Execute method that will be called when the tool is invoked
21+
* @param request The validated request parameters
22+
*/
23+
public async execute({ name }: z.infer<typeof parameters>) {
24+
return {
25+
content: [
26+
{
27+
type: 'text',
28+
text: `Hello, ${name}! Nice to meet you.`,
29+
},
30+
],
31+
};
32+
}
33+
}
34+
35+
// Initialize a new MCP server with the name 'greet-server'
36+
const server = new McpServer({ name: 'greet-server' });
37+
38+
// Create an instance of the GreetTool class
39+
const greetTool = new GreetTool();
40+
41+
// Register the tool with the server
42+
server.tool(greetTool);
43+
44+
// Start the server using stdio as the transport method
45+
server.start({ transportType: 'stdio' });

examples/greet.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
import { McpServer, type McpTool } from 'simple-mcp';
12
import { z } from 'zod';
23

3-
import { McpServer, type McpTool } from '../dist';
4-
54
// Initialize a new MCP server with the name 'greet-server'
65
const server = new McpServer({ name: 'greet-server' });
76

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"eslint": "8.57.1",
5050
"eslint-plugin-simple-import-sort": "12.1.1",
5151
"prettier": "3.3.3",
52+
"simple-mcp": "^0.0.1",
5253
"tsup": "^8.4.0",
5354
"typescript": "^5.8.2"
5455
}

0 commit comments

Comments
 (0)