Skip to content

Commit f3d25d9

Browse files
committed
Fix errors when title is not specified
Since the optional `title` becomes `title: nil` when not provided, the following error occurs. This occurs with Claude Code 1.0.100. ```console [DEBUG] MCP server "example": Connection failed after 1591ms: [ { "code": "invalid_type", "expected": "string", "received": "null", "path": [ "serverInfo", "title" ], "message": "Expected string, received null" } ] ``` By adding `compact`, the `title` key will be removed if the optional `title` keyword argument is not specified.
1 parent eb0d9c0 commit f3d25d9

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

lib/mcp/server.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def server_info
222222
name:,
223223
title:,
224224
version:,
225-
}
225+
}.compact
226226
end
227227

228228
def init(request)

lib/mcp/tool.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def to_h
1919
title: title_value,
2020
description: description_value,
2121
inputSchema: input_schema_value.to_h,
22-
}
22+
}.compact
2323
result[:annotations] = annotations_value.to_h if annotations_value
2424
result
2525
end

test/mcp/server_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ def call(message:, server_context: nil)
771771
assert_equal Configuration::DEFAULT_PROTOCOL_VERSION, response[:result][:protocolVersion]
772772
end
773773

774-
test "server uses default title when not configured" do
774+
test "server does not have title when not configured" do
775775
server = Server.new(name: "test_server")
776776
request = {
777777
jsonrpc: "2.0",
@@ -780,7 +780,7 @@ def call(message:, server_context: nil)
780780
}
781781

782782
response = server.handle(request)
783-
assert_nil response[:result][:serverInfo][:title]
783+
refute response[:result][:serverInfo].key?(:title)
784784
end
785785

786786
test "server uses default version when not configured" do

test/mcp/tool_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ def call(message:, server_context: nil)
3333
assert_equal({ name: "mock_tool", title: "Mock Tool", description: "a mock tool for testing", inputSchema: { type: "object" } }, tool.to_h)
3434
end
3535

36+
test "#to_h does not have `:title` key when title is omitted" do
37+
tool = Tool.define(
38+
name: "mock_tool",
39+
description: "a mock tool for testing",
40+
)
41+
refute tool.to_h.key?(:title)
42+
end
43+
3644
test "#to_h includes annotations when present" do
3745
tool = TestTool
3846
expected_annotations = {

0 commit comments

Comments
 (0)