You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -9,28 +9,27 @@ Tools are a powerful primitive in the Model Context Protocol (MCP) that enable s
9
9
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).
10
10
</Note>
11
11
12
-
## How tools work
12
+
## Overview
13
13
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:
15
15
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.
20
21
21
22
## Tool definition structure
22
23
23
24
Each tool is defined with the following structure:
4. Tool definitions can be updated (though this should be done carefully)
221
205
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
243
207
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:
249
209
250
-
## Error handling
210
+
1. Set `isError` to `true` in the result
211
+
2. Include error details in the `content` array
251
212
252
-
Tools should return clear error information:
213
+
Here's an example of proper error handling for tools:
253
214
254
215
```typescript
255
216
try {
256
217
// Tool operation
218
+
const result =performOperation();
219
+
return {
220
+
content: [
221
+
{
222
+
type: "text",
223
+
text: `Operation successful: ${result}`
224
+
}
225
+
]
226
+
};
257
227
} catch (error) {
258
228
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}`
264
234
}
265
-
}
235
+
]
266
236
};
267
237
}
268
238
```
269
239
240
+
This approach allows the LLM to see that an error occurred and potentially take corrective action or request human intervention.
241
+
270
242
## Testing tools
271
243
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