Something went wrong. Try again.
Read-only, events-only MCP server for GitHub
Something went wrong. Try again.
github-events-mcp server_test.go
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243package main
import ( "bufio" "context" "encoding/json" "errors" "os" "reflect" "sync" "testing" "time"
"github.com/modelcontextprotocol/go-sdk/jsonrpc" "github.com/modelcontextprotocol/go-sdk/mcp")
type eventWireResponse struct { JSONRPC string `json:"jsonrpc"` ID int `json:"id"` Result json.RawMessage `json:"result"` Error *jsonrpc.Error `json:"error"`}
type eventWireClient struct { t *testing.T input *os.File output *bufio.Reader outputFile *os.File ports []*os.File cancel context.CancelFunc done chan error once sync.Once nextID int}
func newEventWireClient(t *testing.T, poller *Poller) *eventWireClient { t.Helper() server, err := newServer(poller) if err != nil { t.Fatal(err) } inputRead, inputWrite, err := os.Pipe() if err != nil { t.Fatal(err) } outputRead, outputWrite, err := os.Pipe() if err != nil { inputRead.Close() inputWrite.Close() t.Fatal(err) } ctx, cancel := context.WithCancel(context.Background()) client := &eventWireClient{ t: t, input: inputWrite, output: bufio.NewReader(outputRead), outputFile: outputRead, ports: []*os.File{inputRead, inputWrite, outputRead, outputWrite}, cancel: cancel, done: make(chan error, 1), } go func() { client.done <- server.Run(ctx, &mcp.IOTransport{Reader: inputRead, Writer: outputWrite, MaxLineLength: 2 << 20}) }() t.Cleanup(client.close) return client}
func (c *eventWireClient) close() { c.once.Do(func() { c.cancel() for _, port := range c.ports { port.Close() } select { case err := <-c.done: if err != nil && !errors.Is(err, context.Canceled) && !errors.Is(err, os.ErrClosed) { c.t.Errorf("server.Run ended with %v", err) } case <-time.After(5 * time.Second): c.t.Error("server goroutine did not stop after closing the transport") } })}
func (c *eventWireClient) request(method string, params map[string]any) eventWireResponse { c.t.Helper() if params == nil { params = make(map[string]any) } params["_meta"] = map[string]any{ mcp.MetaKeyProtocolVersion: "2026-07-28", mcp.MetaKeyClientInfo: map[string]any{"name": "protocol-test", "version": "1"}, mcp.MetaKeyClientCapabilities: map[string]any{"extensions": map[string]any{"io.modelcontextprotocol/events": map[string]any{}}}, } c.nextID++ request := map[string]any{"jsonrpc": "2.0", "id": c.nextID, "method": method, "params": params} deadline := time.Now().Add(5 * time.Second) if err := c.input.SetWriteDeadline(deadline); err != nil { c.t.Fatal(err) } if err := c.outputFile.SetReadDeadline(deadline); err != nil { c.t.Fatal(err) } if err := json.NewEncoder(c.input).Encode(request); err != nil { c.t.Fatal(err) } line, err := c.output.ReadBytes('\n') if err != nil { c.t.Fatalf("reading %s response: %v", method, err) } var response eventWireResponse if err := json.Unmarshal(line, &response); err != nil { c.t.Fatalf("invalid newline JSON response %q: %v", line, err) } if response.JSONRPC != "2.0" || response.ID != c.nextID { c.t.Fatalf("incorrect response envelope: %+v", response) } return response}
func (c *eventWireClient) call(method string, params map[string]any, result any) { c.t.Helper() response := c.request(method, params) if response.Error != nil { c.t.Fatalf("%s error: %v", method, response.Error) } var shape struct { ResultType string `json:"resultType"` } if err := json.Unmarshal(response.Result, &shape); err != nil { c.t.Fatal(err) } if shape.ResultType != "complete" { c.t.Fatalf("%s resultType = %q, want complete", method, shape.ResultType) } if err := json.Unmarshal(response.Result, result); err != nil { c.t.Fatal(err) }}
func TestServerDiscoverAndEventList(t *testing.T) { clock := &pollTestClock{at: protocolTestTime} source := &pollTestSource{} client := newEventWireClient(t, testPoller(source, clock)) var discovery struct { SupportedVersions []string `json:"supportedVersions"` Capabilities *mcp.ServerCapabilities `json:"capabilities"` Instructions string `json:"instructions"` } client.call("server/discover", nil, &discovery) if !reflect.DeepEqual(discovery.SupportedVersions, []string{"2026-07-28"}) || discovery.Capabilities == nil { t.Fatalf("incorrect discovery: %+v", discovery) } settings, ok := discovery.Capabilities.Extensions["io.modelcontextprotocol/events"].(map[string]any) if !ok || len(settings) != 0 || discovery.Instructions == "" { t.Fatalf("missing events extension or instructions: %+v", discovery) } var listed ListResult client.call("events/list", nil, &listed) if len(listed.Events) != 2 { t.Fatalf("event descriptors = %+v", listed.Events) } for i, name := range []string{activityName, notificationsName} { descriptor := listed.Events[i] if descriptor.Name != name || !reflect.DeepEqual(descriptor.Delivery, []string{"poll"}) || descriptor.InputSchema["type"] != "object" || descriptor.InputSchema["additionalProperties"] != false || descriptor.PayloadSchema["type"] != "object" { t.Fatalf("invalid descriptor: %+v", descriptor) } } response := client.request("events/list", map[string]any{"cursor": "unsupported"}) if response.Error == nil || response.Error.Code != jsonrpc.CodeInvalidParams { t.Fatalf("unexpected list cursor response: %+v", response) } if len(source.history()) != 0 { t.Fatal("discover/list reached GitHub source") }}
func TestServerPollRoundTripAndRestart(t *testing.T) { clock := &pollTestClock{at: protocolTestTime} initialTime := protocolTestTime firstEvent := testEvent("first wire event", initialTime.Add(30*time.Second)) secondEvent := testEvent("second wire event", initialTime.Add(90*time.Second)) source := &pollTestSource{fetch: func(context.Context, string, Arguments, time.Time) (FetchResult, error) { return FetchResult{Complete: true, Events: []Event{firstEvent}}, nil }} client := newEventWireClient(t, testPoller(source, clock)) var initial PollResult client.call("events/poll", map[string]any{"name": notificationsName, "cursor": nil}, &initial) if initial.Events == nil || len(initial.Events) != 0 || initial.Cursor == "" || initial.Truncated || initial.HasMore || len(source.history()) != 0 { t.Fatalf("null-cursor poll should only create a cursor: %+v", initial) } clock.advance(time.Minute) var first PollResult client.call("events/poll", map[string]any{"name": notificationsName, "cursor": initial.Cursor}, &first) if len(first.Events) != 1 || first.Events[0].EventID != firstEvent.EventID || !first.Events[0].Timestamp.Equal(firstEvent.Timestamp) || string(first.Events[0].Data) != string(firstEvent.Data) { t.Fatalf("wire event changed or disappeared: %+v", first) } var duplicate PollResult client.call("events/poll", map[string]any{"name": notificationsName, "cursor": first.Cursor}, &duplicate) if len(duplicate.Events) != 0 || len(source.history()) != 1 { t.Fatalf("duplicate/refetch on wire: %+v", duplicate) } client.close()
clock.advance(time.Minute) restartedSource := &pollTestSource{fetch: func(context.Context, string, Arguments, time.Time) (FetchResult, error) { return FetchResult{Complete: true, Events: []Event{firstEvent, secondEvent}}, nil }} restarted := newEventWireClient(t, testPoller(restartedSource, clock)) var resumed PollResult restarted.call("events/poll", map[string]any{"name": notificationsName, "cursor": first.Cursor}, &resumed) if len(resumed.Events) != 1 || resumed.Events[0].EventID != secondEvent.EventID || resumed.Truncated { t.Fatalf("server restart did not resume portable cursor: %+v", resumed) }}
func TestServerPollValidationAndAuthorization(t *testing.T) { clock := &pollTestClock{at: protocolTestTime} source := &pollTestSource{fetch: func(context.Context, string, Arguments, time.Time) (FetchResult, error) { return FetchResult{}, rpcError(-32024, "Forbidden", nil) }} client := newEventWireClient(t, testPoller(source, clock)) for _, params := range []map[string]any{ {"name": activityName, "arguments": map[string]any{"repos": nil}}, {"name": activityName, "arguments": map[string]any{"authors": 42}}, {"name": notificationsName, "maxEvents": 0}, {"name": notificationsName, "maxAgeMs": -1}, {"name": notificationsName, "cursor": "broken"}, } { response := client.request("events/poll", params) if response.Error == nil || response.Error.Code != jsonrpc.CodeInvalidParams { t.Fatalf("accepted invalid wire params: %+v", response) } } if len(source.history()) != 0 { t.Fatal("invalid wire params fetched upstream") } cursor := testCursor(notificationsName, nil, protocolTestTime.Add(-time.Minute)) response := client.request("events/poll", map[string]any{"name": notificationsName, "cursor": cursor}) if response.Error == nil || response.Error.Code != -32024 || response.Error.Message != "Forbidden" || len(response.Result) != 0 { t.Fatalf("authorization error was not preserved on the wire: %+v", response) }}