ruby-sdk/lib/mcp/tool/response.rb
Koichi ITO 0034952df9 Set default content to an empty array instead of nil
Follow-up to https://github.com/modelcontextprotocol/ruby-sdk/pull/147.

According to the specification schema, `content` is not optional and therefore cannot be omitted:

```typescript
interface CallToolResult {
  _meta?: { [key: string]: unknown };
  content: ContentBlock[];
  isError?: boolean;
  structuredContent?: { [key: string]: unknown };
  [key: string]: unknown;
}
```

https://modelcontextprotocol.io/specification/2025-06-18/schema#calltoolresult

Instead of `nil`, an empty array is set as the default value.

There may be a better value for `content`, but at the very least it should not be missing.
2025-10-02 23:52:19 +09:00

30 lines
818 B
Ruby

# frozen_string_literal: true
module MCP
class Tool
class Response
NOT_GIVEN = Object.new.freeze
attr_reader :content, :structured_content
def initialize(content = nil, deprecated_error = NOT_GIVEN, error: false, structured_content: nil)
if deprecated_error != NOT_GIVEN
warn("Passing `error` with the 2nd argument of `Response.new` is deprecated. Use keyword argument like `Response.new(content, error: error)` instead.", uplevel: 1)
error = deprecated_error
end
@content = content || []
@error = error
@structured_content = structured_content
end
def error?
!!@error
end
def to_h
{ content:, isError: error?, structuredContent: @structured_content }.compact
end
end
end
end