Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions internal/jsonrpc2/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/json"
"errors"
"fmt"
"math"

internaljson "github.com/modelcontextprotocol/go-sdk/internal/json"
)
Expand All @@ -21,8 +22,9 @@ type ID struct {

// MakeID coerces the given Go value to an ID. The value should be the JSON
// decoding of a Request identifier: nil, float64, json.Number, or string.
// Decoding with json.Number keeps integer IDs above 2^53 exact; a json.Number
// must be an integer literal in the int64 range.
// Decoding with json.Number keeps integer IDs above 2^53 exact; a float64 or
// json.Number must hold an integer in the int64 range, and a fractional or
// out-of-range value is rejected instead of being truncated.
//
// Returns an error if the value type was not a valid Request ID type.
//
Expand All @@ -34,14 +36,14 @@ func MakeID(v any) (ID, error) {
case nil:
return ID{}, nil
case float64:
return Int64ID(int64(v)), nil
return int64ID(v)
case json.Number:
if i, err := v.Int64(); err == nil {
return Int64ID(i), nil
}
// Fallback to maintain backward compatibility
if f, err := v.Float64(); err == nil {
return Int64ID(int64(f)), nil
return int64ID(f)
}
return ID{}, fmt.Errorf("%w: invalid numeric ID %q", ErrParse, v)
case string:
Expand All @@ -50,6 +52,17 @@ func MakeID(v any) (ID, error) {
return ID{}, fmt.Errorf("%w: invalid ID type %T", ErrParse, v)
}

// int64ID converts a numeric ID that holds an integer in the int64 range, and
// rejects everything else. Truncating a fractional ID would answer a request
// with an ID the client never sent, and converting a float outside the int64
// range to int64 has an implementation-defined result.
func int64ID(f float64) (ID, error) {
if f != math.Trunc(f) || f < math.MinInt64 || f >= math.MaxInt64 {
return ID{}, fmt.Errorf("%w: invalid numeric ID %v", ErrParse, f)
}
return Int64ID(int64(f)), nil
}

// Message is the interface to all jsonrpc2 message types.
// They share no common functionality, but are a closed set of concrete types
// that are allowed to implement this interface. The message types are *Request
Expand Down
93 changes: 93 additions & 0 deletions internal/jsonrpc2/messages_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2026 The Go MCP SDK Authors. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package jsonrpc2

import (
"encoding/json"
"errors"
"math"
"testing"
)

func TestMakeID(t *testing.T) {
tests := []struct {
name string
in any
want any // nil, int64 or string
wantErr bool
}{
{name: "nil", in: nil},
{name: "string", in: "abc", want: "abc"},
{name: "float64 integer", in: float64(42), want: int64(42)},
{name: "float64 negative integer", in: float64(-42), want: int64(-42)},
{name: "float64 fraction", in: 1.5, wantErr: true},
{name: "float64 above range", in: 1e30, wantErr: true},
{name: "float64 below range", in: -1e30, wantErr: true},
{name: "float64 NaN", in: math.NaN(), wantErr: true},
{name: "float64 infinity", in: math.Inf(1), wantErr: true},
{name: "json.Number integer", in: json.Number("42"), want: int64(42)},
{name: "json.Number exponent", in: json.Number("1e3"), want: int64(1000)},
{name: "json.Number fraction", in: json.Number("1.5"), wantErr: true},
{name: "json.Number above range", in: json.Number("1e30"), wantErr: true},
{name: "json.Number invalid", in: json.Number("abc"), wantErr: true},
{name: "bool", in: true, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
id, err := MakeID(tt.in)
if gotErr := err != nil; gotErr != tt.wantErr {
t.Fatalf("MakeID(%v) error = %v, wantErr %v", tt.in, err, tt.wantErr)
}
if err != nil {
if !errors.Is(err, ErrParse) {
t.Fatalf("MakeID(%v) error = %v, want a parse error", tt.in, err)
}
return
}
switch want := tt.want.(type) {
case nil:
if id != (ID{}) {
t.Fatalf("MakeID(%v) = %v, want the zero ID", tt.in, id)
}
case int64:
if id != Int64ID(want) {
t.Fatalf("MakeID(%v) = %v, want %v", tt.in, id, Int64ID(want))
}
case string:
if id != StringID(want) {
t.Fatalf("MakeID(%v) = %v, want %v", tt.in, id, StringID(want))
}
}
})
}
}

// A fractional ID used to be truncated to the integer part, so a response
// carried an ID the client never sent; an out-of-range one became a random
// int64. Both have to fail the message instead.
func TestDecodeMessageRejectsNonIntegerID(t *testing.T) {
for _, data := range []string{
`{"jsonrpc":"2.0","id":1.5,"method":"m"}`,
`{"jsonrpc":"2.0","id":1e30,"method":"m"}`,
} {
if _, err := DecodeMessage([]byte(data)); !errors.Is(err, ErrParse) {
t.Errorf("DecodeMessage(%s) error = %v, want a parse error", data, err)
}
}
}

func TestDecodeMessageKeepsIntegerID(t *testing.T) {
msg, err := DecodeMessage([]byte(`{"jsonrpc":"2.0","id":42,"method":"m"}`))
if err != nil {
t.Fatalf("DecodeMessage: %v", err)
}
req, ok := msg.(*Request)
if !ok {
t.Fatalf("DecodeMessage returned %T, want *Request", msg)
}
if req.ID != Int64ID(42) {
t.Fatalf("ID = %v, want %v", req.ID, Int64ID(42))
}
}
Loading