@@ -1946,3 +1946,79 @@ func TestMCPServer_ToolCapabilitiesBehavior(t *testing.T) {
19461946 })
19471947 }
19481948}
1949+
1950+ func TestMCPServer_ProtocolNegotiation (t * testing.T ) {
1951+ tests := []struct {
1952+ name string
1953+ clientVersion string
1954+ expectedVersion string
1955+ }{
1956+ {
1957+ name : "Server supports client version - should respond with same version" ,
1958+ clientVersion : "2024-11-05" ,
1959+ expectedVersion : "2024-11-05" , // Server must respond with client's version if supported
1960+ },
1961+ {
1962+ name : "Client requests current latest - should respond with same version" ,
1963+ clientVersion : mcp .LATEST_PROTOCOL_VERSION , // "2025-03-26"
1964+ expectedVersion : mcp .LATEST_PROTOCOL_VERSION ,
1965+ },
1966+ {
1967+ name : "Client requests unsupported future version - should respond with server's latest" ,
1968+ clientVersion : "2026-01-01" , // Future unsupported version
1969+ expectedVersion : mcp .LATEST_PROTOCOL_VERSION , // Server responds with its latest supported
1970+ },
1971+ {
1972+ name : "Client requests unsupported old version - should respond with server's latest" ,
1973+ clientVersion : "2023-01-01" , // Very old unsupported version
1974+ expectedVersion : mcp .LATEST_PROTOCOL_VERSION , // Server responds with its latest supported
1975+ },
1976+ }
1977+
1978+ for _ , tt := range tests {
1979+ t .Run (tt .name , func (t * testing.T ) {
1980+ server := NewMCPServer ("test-server" , "1.0.0" )
1981+
1982+ params := struct {
1983+ ProtocolVersion string `json:"protocolVersion"`
1984+ ClientInfo mcp.Implementation `json:"clientInfo"`
1985+ Capabilities mcp.ClientCapabilities `json:"capabilities"`
1986+ }{
1987+ ProtocolVersion : tt .clientVersion ,
1988+ ClientInfo : mcp.Implementation {
1989+ Name : "test-client" ,
1990+ Version : "1.0.0" ,
1991+ },
1992+ }
1993+
1994+ // Create initialize request with specific protocol version
1995+ initRequest := mcp.JSONRPCRequest {
1996+ JSONRPC : "2.0" ,
1997+ ID : mcp .NewRequestId (int64 (1 )),
1998+ Request : mcp.Request {
1999+ Method : "initialize" ,
2000+ },
2001+ Params : params ,
2002+ }
2003+
2004+ messageBytes , err := json .Marshal (initRequest )
2005+ assert .NoError (t , err )
2006+
2007+ response := server .HandleMessage (context .Background (), messageBytes )
2008+ assert .NotNil (t , response )
2009+
2010+ resp , ok := response .(mcp.JSONRPCResponse )
2011+ assert .True (t , ok )
2012+
2013+ initResult , ok := resp .Result .(mcp.InitializeResult )
2014+ assert .True (t , ok )
2015+
2016+ assert .Equal (
2017+ t ,
2018+ tt .expectedVersion ,
2019+ initResult .ProtocolVersion ,
2020+ "Protocol version should follow MCP spec negotiation rules" ,
2021+ )
2022+ })
2023+ }
2024+ }
0 commit comments