Skip to content

Commit 2cace9a

Browse files
committed
Update tools.mdx
1 parent 75b3284 commit 2cace9a

File tree

1 file changed

+89
-122
lines changed

1 file changed

+89
-122
lines changed

docs/concepts/tools.mdx

Lines changed: 89 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,27 @@ Tools are a powerful primitive in the Model Context Protocol (MCP) that enable s
99
Tools are designed to be **model-controlled**, meaning that tools are exposed from servers to clients with the intention of the AI model being able to automatically invoke them (with a human in the loop to grant approval).
1010
</Note>
1111

12-
## How tools work
12+
## Overview
1313

14-
Tools in MCP follow a request-response pattern where:
14+
Tools in MCP allow servers to expose executable functions that can be invoked by clients and used by LLMs to perform actions. Key aspects of tools include:
1515

16-
1. Clients discover available tools through the `tools/list` endpoint
17-
2. Clients can invoke tools using the `tools/call` endpoint
18-
3. Servers execute the requested operation and return results
19-
4. Progress updates can be sent during long-running operations
16+
- **Discovery**: Clients can list available tools through the `tools/list` endpoint
17+
- **Invocation**: Tools are called using the `tools/call` endpoint, where servers perform the requested operation and return results
18+
- **Flexibility**: Tools can range from simple calculations to complex API interactions
19+
20+
Like [resources](/docs/concepts/resources), tools are identified by unique names and can include descriptions to guide their usage. However, unlike resources, tools represent dynamic operations that can modify state or interact with external systems.
2021

2122
## Tool definition structure
2223

2324
Each tool is defined with the following structure:
2425

2526
```typescript
2627
{
27-
name: string; // Unique identifier for the tool
28+
name: string; // Unique identifier for the tool
2829
description?: string; // Human-readable description
29-
inputSchema: { // JSON Schema for the tool's parameters
30+
inputSchema: { // JSON Schema for the tool's parameters
3031
type: "object",
31-
properties: {
32-
// Tool-specific parameters
33-
}
32+
properties: { ... } // Tool-specific parameters
3433
}
3534
}
3635
```
@@ -39,73 +38,58 @@ Each tool is defined with the following structure:
3938

4039
Here's an example of implementing a basic tool in an MCP server:
4140

42-
```typescript
43-
const server = new Server({
44-
name: "example-server",
45-
version: "1.0.0"
46-
});
47-
48-
// Define available tools
49-
server.setRequestHandler(ListToolsRequestSchema, async () => {
50-
return {
51-
tools: [{
52-
name: "calculate_sum",
53-
description: "Add two numbers together",
54-
inputSchema: {
55-
type: "object",
56-
properties: {
57-
a: { type: "number" },
58-
b: { type: "number" }
59-
},
60-
required: ["a", "b"]
41+
<Tabs>
42+
<Tab title="TypeScript">
43+
```typescript
44+
const server = new Server({
45+
name: "example-server",
46+
version: "1.0.0"
47+
}, {
48+
capabilities: {
49+
tools: {}
6150
}
62-
}]
63-
};
64-
});
65-
66-
// Handle tool execution
67-
server.setRequestHandler(CallToolRequestSchema, async (request) => {
68-
if (request.params.name === "calculate_sum") {
69-
const { a, b } = request.params.arguments;
70-
return {
71-
toolResult: a + b
72-
};
73-
}
74-
throw new Error("Tool not found");
75-
});
76-
```
77-
78-
## Progress reporting
79-
80-
For long-running operations, tools can report progress using the progress notification system:
81-
82-
```typescript
83-
server.setRequestHandler(CallToolRequestSchema, async (request) => {
84-
if (request.params.name === "long_process") {
85-
const progressToken = request.params._meta?.progressToken;
86-
87-
for (let i = 0; i < total; i++) {
88-
await doWork();
89-
90-
// Send progress update
91-
await server.notification({
92-
method: "notifications/progress",
93-
params: {
94-
progress: i,
95-
total: total,
96-
progressToken
97-
}
98-
});
99-
}
100-
101-
return { toolResult: "Complete" };
102-
}
103-
});
104-
```
51+
});
52+
53+
// Define available tools
54+
server.setRequestHandler(ListToolsRequestSchema, async () => {
55+
return {
56+
tools: [{
57+
name: "calculate_sum",
58+
description: "Add two numbers together",
59+
inputSchema: {
60+
type: "object",
61+
properties: {
62+
a: { type: "number" },
63+
b: { type: "number" }
64+
},
65+
required: ["a", "b"]
66+
}
67+
}]
68+
};
69+
});
70+
71+
// Handle tool execution
72+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
73+
if (request.params.name === "calculate_sum") {
74+
const { a, b } = request.params.arguments;
75+
return {
76+
toolResult: a + b
77+
};
78+
}
79+
throw new Error("Tool not found");
80+
});
81+
```
82+
</Tab>
83+
<Tab title="Python">
84+
```python
85+
# Python implementation coming soon
86+
```
87+
</Tab>
88+
</Tabs>
10589

106-
## Common tool patterns
90+
## Example tool patterns
10791

108-
Here are some common patterns for implementing tools:
92+
Here are some examples of types of tools that a server could provide:
10993

11094
### System operations
11195

@@ -173,7 +157,7 @@ When implementing tools:
173157

174158
1. Provide clear, descriptive names and descriptions
175159
2. Use detailed JSON Schema definitions for parameters
176-
3. Include examples in tool descriptions when helpful
160+
3. Include examples in tool descriptions to demonstrate how the model should use them
177161
4. Implement proper error handling and validation
178162
5. Use progress reporting for long operations
179163
6. Keep tool operations focused and atomic
@@ -219,65 +203,48 @@ MCP supports dynamic tool discovery:
219203
3. Tools can be added or removed during runtime
220204
4. Tool definitions can be updated (though this should be done carefully)
221205

222-
## Common tool categories
223-
224-
Tools typically fall into these categories:
225-
226-
### System integration
227-
- File operations
228-
- Process management
229-
- System information
230-
- Network operations
231-
232-
### External services
233-
- API calls
234-
- Database operations
235-
- Authentication services
236-
- Cloud services
237-
238-
### Data processing
239-
- Format conversion
240-
- Data analysis
241-
- Content generation
242-
- Media processing
206+
## Error handling
243207

244-
### Device control
245-
- Hardware interaction
246-
- Device configuration
247-
- Sensor reading
248-
- Output control
208+
Tool errors should be reported within the result object, not as MCP protocol-level errors. This allows the LLM to see and potentially handle the error. When a tool encounters an error:
249209

250-
## Error handling
210+
1. Set `isError` to `true` in the result
211+
2. Include error details in the `content` array
251212

252-
Tools should return clear error information:
213+
Here's an example of proper error handling for tools:
253214

254215
```typescript
255216
try {
256217
// Tool operation
218+
const result = performOperation();
219+
return {
220+
content: [
221+
{
222+
type: "text",
223+
text: `Operation successful: ${result}`
224+
}
225+
]
226+
};
257227
} catch (error) {
258228
return {
259-
toolResult: {
260-
error: {
261-
code: "OPERATION_FAILED",
262-
message: "Failed to process request",
263-
details: error.message
229+
isError: true,
230+
content: [
231+
{
232+
type: "text",
233+
text: `Error: ${error.message}`
264234
}
265-
}
235+
]
266236
};
267237
}
268238
```
269239

240+
This approach allows the LLM to see that an error occurred and potentially take corrective action or request human intervention.
241+
270242
## Testing tools
271243

272-
Consider these testing approaches:
273-
274-
1. Unit test parameter validation
275-
2. Mock external dependencies
276-
3. Test progress reporting
277-
4. Verify error handling
278-
5. Test concurrent execution
279-
6. Validate return values
280-
7. Test rate limiting
281-
8. Check resource cleanup
282-
9. Test timeout handling
283-
10. Verify security constraints
244+
A comprehensive testing strategy for MCP tools should cover:
245+
246+
- **Functional testing**: Verify tools execute correctly with valid inputs and handle invalid inputs appropriately
247+
- **Integration testing**: Test tool interaction with external systems using both real and mocked dependencies
248+
- **Security testing**: Validate authentication, authorization, input sanitization, and rate limiting
249+
- **Performance testing**: Check behavior under load, timeout handling, and resource cleanup
250+
- **Error handling**: Ensure tools properly report errors through the MCP protocol and clean up resources

0 commit comments

Comments
 (0)