Skip to content

Commit 200826c

Browse files
authored
Merge pull request #247 from koic/fix_default_handler_return_values
Fix default handler return values to comply with MCP spec
2 parents 76ab822 + 7b910bc commit 200826c

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed

lib/mcp/server.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ def initialize(
9999
Methods::LOGGING_SET_LEVEL => method(:configure_logging_level),
100100

101101
# No op handlers for currently unsupported methods
102-
Methods::RESOURCES_SUBSCRIBE => ->(_) {},
103-
Methods::RESOURCES_UNSUBSCRIBE => ->(_) {},
104-
Methods::COMPLETION_COMPLETE => ->(_) {},
102+
Methods::RESOURCES_SUBSCRIBE => ->(_) { {} },
103+
Methods::RESOURCES_UNSUBSCRIBE => ->(_) { {} },
104+
Methods::COMPLETION_COMPLETE => ->(_) { { completion: { values: [], hasMore: false } } },
105105
Methods::ELICITATION_CREATE => ->(_) {},
106106
}
107107
@transport = transport

test/mcp/server_test.rb

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,6 +1578,87 @@ class Example < Tool
15781578
assert_includes response[:result][:content][0][:text], "Invalid arguments"
15791579
end
15801580

1581+
test "#handle completion/complete returns default completion result" do
1582+
server = Server.new(
1583+
name: "test_server",
1584+
capabilities: { completions: {} },
1585+
)
1586+
1587+
server.handle({ jsonrpc: "2.0", method: "initialize", id: 1 })
1588+
server.handle({ jsonrpc: "2.0", method: "notifications/initialized" })
1589+
1590+
response = server.handle({
1591+
jsonrpc: "2.0",
1592+
id: 2,
1593+
method: "completion/complete",
1594+
params: {
1595+
ref: { type: "ref/prompt", name: "test" },
1596+
argument: { name: "arg", value: "val" },
1597+
},
1598+
})
1599+
1600+
assert_equal(
1601+
{
1602+
jsonrpc: "2.0",
1603+
id: 2,
1604+
result: { completion: { values: [], hasMore: false } },
1605+
},
1606+
response,
1607+
)
1608+
end
1609+
1610+
test "#handle resources/subscribe returns empty result" do
1611+
server = Server.new(
1612+
name: "test_server",
1613+
capabilities: { resources: { subscribe: true } },
1614+
)
1615+
1616+
server.handle({ jsonrpc: "2.0", method: "initialize", id: 1 })
1617+
server.handle({ jsonrpc: "2.0", method: "notifications/initialized" })
1618+
1619+
response = server.handle({
1620+
jsonrpc: "2.0",
1621+
id: 2,
1622+
method: "resources/subscribe",
1623+
params: { uri: "https://example.com/resource" },
1624+
})
1625+
1626+
assert_equal(
1627+
{
1628+
jsonrpc: "2.0",
1629+
id: 2,
1630+
result: {},
1631+
},
1632+
response,
1633+
)
1634+
end
1635+
1636+
test "#handle resources/unsubscribe returns empty result" do
1637+
server = Server.new(
1638+
name: "test_server",
1639+
capabilities: { resources: { subscribe: true } },
1640+
)
1641+
1642+
server.handle({ jsonrpc: "2.0", method: "initialize", id: 1 })
1643+
server.handle({ jsonrpc: "2.0", method: "notifications/initialized" })
1644+
1645+
response = server.handle({
1646+
jsonrpc: "2.0",
1647+
id: 2,
1648+
method: "resources/unsubscribe",
1649+
params: { uri: "https://example.com/resource" },
1650+
})
1651+
1652+
assert_equal(
1653+
{
1654+
jsonrpc: "2.0",
1655+
id: 2,
1656+
result: {},
1657+
},
1658+
response,
1659+
)
1660+
end
1661+
15811662
test "tools/call with no args" do
15821663
server = Server.new(tools: [@tool_with_no_args])
15831664

0 commit comments

Comments
 (0)