Skip to content

Commit 64332dd

Browse files
committed
feat: add example tool implementation
1 parent 0c4fc33 commit 64332dd

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

src/tools/example.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @fileoverview Unit tests for the example tool
3+
* @module tools/example.test
4+
*/
5+
6+
import { describe, expect, it } from 'vitest';
7+
import { exampleTool } from './example.js';
8+
9+
/**
10+
* Test suite for the example tool functionality
11+
*/
12+
describe('exampleTool', () => {
13+
/**
14+
* Test that the tool correctly echoes back the input message
15+
*/
16+
it('should echo the message', async () => {
17+
const result = await exampleTool({ message: 'Hello, world!' });
18+
expect(result.content[0]).toEqual({
19+
type: 'text',
20+
text: 'Echo: Hello, world!',
21+
});
22+
});
23+
24+
/**
25+
* Test that the uppercase option works correctly
26+
*/
27+
it('should convert to uppercase when requested', async () => {
28+
const result = await exampleTool({
29+
message: 'Hello, world!',
30+
uppercase: true,
31+
});
32+
expect(result.content[0]).toEqual({
33+
type: 'text',
34+
text: 'Echo: HELLO, WORLD!',
35+
});
36+
});
37+
38+
/**
39+
* Test that invalid inputs are properly rejected with validation errors
40+
*/
41+
it('should validate input', async () => {
42+
await expect(exampleTool({})).rejects.toThrow();
43+
await expect(exampleTool({ message: 123 })).rejects.toThrow();
44+
});
45+
});

src/tools/example.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @fileoverview Example tool implementation demonstrating MCP tool structure
3+
* @module tools/example
4+
*/
5+
6+
import { z } from 'zod';
7+
8+
/**
9+
* Input schema for the example tool
10+
* @description Validates input parameters for the echo tool
11+
*/
12+
export const ExampleToolSchema = z.object({
13+
message: z.string().describe('The message to echo back'),
14+
uppercase: z
15+
.boolean()
16+
.optional()
17+
.default(false)
18+
.describe('Whether to return the message in uppercase'),
19+
});
20+
21+
/**
22+
* TypeScript type for the example tool input
23+
*/
24+
export type ExampleToolInput = z.infer<typeof ExampleToolSchema>;
25+
26+
/**
27+
* Example tool that echoes back the input message
28+
* @param args - Raw input arguments to be validated against ExampleToolSchema
29+
* @returns MCP tool response with the echoed message
30+
* @throws {z.ZodError} If input validation fails
31+
*
32+
* @example
33+
* ```typescript
34+
* const result = await exampleTool({ message: "Hello", uppercase: true });
35+
* // Returns: { content: [{ type: 'text', text: 'Echo: HELLO' }] }
36+
* ```
37+
*/
38+
export async function exampleTool(args: unknown) {
39+
const input = ExampleToolSchema.parse(args);
40+
41+
let result = input.message;
42+
if (input.uppercase) {
43+
result = result.toUpperCase();
44+
}
45+
46+
return {
47+
content: [
48+
{
49+
type: 'text',
50+
text: `Echo: ${result}`,
51+
},
52+
],
53+
};
54+
}

0 commit comments

Comments
 (0)