Rename APITool to WeatherTool and add error handling
Updated the APITool class to WeatherTool and modified method parameters. Added section on tool responses with errors.
This commit is contained in:
parent
47c70ef8e7
commit
2b205581ea
1 changed files with 28 additions and 2 deletions
30
README.md
30
README.md
|
|
@ -597,10 +597,10 @@ Tools can return structured data alongside text content using the `structured_co
|
||||||
The structured content will be included in the JSON-RPC response as the `structuredContent` field.
|
The structured content will be included in the JSON-RPC response as the `structuredContent` field.
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class APITool < MCP::Tool
|
class WeatherTool < MCP::Tool
|
||||||
description "Get current weather and return structured data"
|
description "Get current weather and return structured data"
|
||||||
|
|
||||||
def self.call(endpoint:, server_context:)
|
def self.call(location:, units: "celsius", server_context:)
|
||||||
# Call weather API and structure the response
|
# Call weather API and structure the response
|
||||||
api_response = WeatherAPI.fetch(location, units)
|
api_response = WeatherAPI.fetch(location, units)
|
||||||
weather_data = {
|
weather_data = {
|
||||||
|
|
@ -622,6 +622,32 @@ class APITool < MCP::Tool
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Tool Responses with Errors
|
||||||
|
|
||||||
|
Tools can return error information alongside text content using the `error` parameter.
|
||||||
|
|
||||||
|
The error will be included in the JSON-RPC response as the `isError` field.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
class WeatherTool < MCP::Tool
|
||||||
|
description "Get current weather and return structured data"
|
||||||
|
|
||||||
|
def self.call(server_context:)
|
||||||
|
# Do something here
|
||||||
|
content = {}
|
||||||
|
|
||||||
|
MCP::Tool::Response.new(
|
||||||
|
[{
|
||||||
|
type: "text",
|
||||||
|
text: content.to_json
|
||||||
|
}],
|
||||||
|
structured_content: content,
|
||||||
|
error: true
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
### Prompts
|
### Prompts
|
||||||
|
|
||||||
MCP spec includes [Prompts](https://modelcontextprotocol.io/specification/2025-06-18/server/prompts), which enable servers to define reusable prompt templates and workflows that clients can easily surface to users and LLMs.
|
MCP spec includes [Prompts](https://modelcontextprotocol.io/specification/2025-06-18/server/prompts), which enable servers to define reusable prompt templates and workflows that clients can easily surface to users and LLMs.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue