mcp: don't mark the server sesion as initialized prematurely

Update ServerSession so that the session is not marked as initialized
until 'notifications/initialized' is actually received from the client.

Include a new test of this lifecycle strictness.

Fixes #225
This commit is contained in:
Rob Findley 2025-08-07 18:46:23 +00:00 • committed by Robert Findley
commit 5ab63feefa
5 changed files with 90 additions and 22 deletions

View file

@ -38,11 +38,12 @@ code_review
"clientInfo": { "name": "ExampleClient", "version": "1.0.0" }
}
}
{"jsonrpc":"2.0", "id": 3, "method": "notifications/initialized"}
{"jsonrpc":"2.0", "method":"ping"}
{"jsonrpc":"2.0", "id": 4, "method": "logging/setLevel"}
{"jsonrpc":"2.0", "id": 5, "method": "completion/complete"}
{"jsonrpc":"2.0", "id": 4, "method": "logging/setLevel", "params": null}
{ "jsonrpc":"2.0", "id": 3, "method": "notifications/initialized" }
{ "jsonrpc":"2.0", "method": "notifications/initialized" }
{ "jsonrpc":"2.0", "method":"ping" }
{ "jsonrpc":"2.0", "id": 4, "method": "logging/setLevel" }
{ "jsonrpc":"2.0", "id": 5, "method": "completion/complete" }
{ "jsonrpc":"2.0", "id": 4, "method": "logging/setLevel", "params": null }
-- server --
{

View file

@ -0,0 +1,57 @@
This test checks that the server obeys the rules for initialization lifecycle,
and rejects non-ping requests until 'initialized' is received.
See also modelcontextprotocol/go-sdk#225.
-- client --
{ "jsonrpc":"2.0", "method": "notifications/initialized" }
{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "2024-11-05",
"capabilities": {},
"clientInfo": { "name": "ExampleClient", "version": "1.0.0" }
}
}
{ "jsonrpc":"2.0", "id": 1, "method":"ping" }
{ "jsonrpc": "2.0", "id": 2, "method": "tools/list" }
{ "jsonrpc":"2.0", "method": "notifications/initialized" }
{ "jsonrpc": "2.0", "id": 3, "method": "tools/list" }
-- server --
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"capabilities": {
"logging": {}
},
"protocolVersion": "2024-11-05",
"serverInfo": {
"name": "testServer",
"version": "v1.0.0"
}
}
}
{
"jsonrpc": "2.0",
"id": 1,
"result": {}
}
{
"jsonrpc": "2.0",
"id": 2,
"error": {
"code": 0,
"message": "method \"tools/list\" is invalid during session initialization"
}
}
{
"jsonrpc": "2.0",
"id": 3,
"result": {
"tools": []
}
}

View file

@ -18,9 +18,11 @@ code_review
"clientInfo": { "name": "ExampleClient", "version": "1.0.0" }
}
}
{ "jsonrpc":"2.0", "method": "notifications/initialized" }
{ "jsonrpc": "2.0", "id": 2, "method": "tools/list" }
{ "jsonrpc": "2.0", "id": 4, "method": "prompts/list" }
{ "jsonrpc": "2.0", "id": 5, "method": "prompts/get" }
-- server --
{
"jsonrpc": "2.0",

View file

@ -21,6 +21,7 @@ info.txt
"clientInfo": { "name": "ExampleClient", "version": "1.0.0" }
}
}
{ "jsonrpc":"2.0", "method": "notifications/initialized" }
{ "jsonrpc": "2.0", "id": 2, "method": "resources/list" }
{
"jsonrpc": "2.0", "id": 3,

View file

@ -20,6 +20,7 @@ greet
"clientInfo": { "name": "ExampleClient", "version": "1.0.0" }
}
}
{"jsonrpc":"2.0", "method": "notifications/initialized"}
{ "jsonrpc": "2.0", "id": 2, "method": "tools/list" }
{ "jsonrpc": "2.0", "id": 3, "method": "resources/list" }
{ "jsonrpc": "2.0", "id": 4, "method": "prompts/list" }