|
| 1 | +//go:build conformance |
| 2 | + |
| 3 | +package conformance_test |
| 4 | + |
| 5 | +import ( |
| 6 | +"bufio" |
| 7 | +"context" |
| 8 | +"encoding/json" |
| 9 | +"fmt" |
| 10 | +"io" |
| 11 | +"os" |
| 12 | +"reflect" |
| 13 | +"strings" |
| 14 | +"testing" |
| 15 | + |
| 16 | +"github.com/docker/docker/api/types/container" |
| 17 | +"github.com/docker/docker/api/types/network" |
| 18 | +"github.com/docker/docker/client" |
| 19 | +"github.com/docker/docker/pkg/stdcopy" |
| 20 | +"github.com/stretchr/testify/require" |
| 21 | +) |
| 22 | + |
| 23 | +typemaintainerstring |
| 24 | + |
| 25 | +const ( |
| 26 | +anthropicmaintainer="anthropic" |
| 27 | +githubmaintainer="github" |
| 28 | +) |
| 29 | + |
| 30 | +typetestLogWriterstruct { |
| 31 | +t*testing.T |
| 32 | +} |
| 33 | + |
| 34 | +func (wtestLogWriter)Write(p []byte) (nint,errerror) { |
| 35 | +w.t.Log(string(p)) |
| 36 | +returnlen(p),nil |
| 37 | +} |
| 38 | + |
| 39 | +funcstart(t*testing.T,mmaintainer)server { |
| 40 | +varimagestring |
| 41 | +ifm==github { |
| 42 | +image="github/github-mcp-server" |
| 43 | +}else { |
| 44 | +image="mcp/github" |
| 45 | +} |
| 46 | + |
| 47 | +ctx:=context.Background() |
| 48 | +dockerClient,err:=client.NewClientWithOpts(client.FromEnv,client.WithAPIVersionNegotiation()) |
| 49 | +require.NoError(t,err) |
| 50 | + |
| 51 | +containerCfg:=&container.Config{ |
| 52 | +OpenStdin:true, |
| 53 | +AttachStdin:true, |
| 54 | +AttachStdout:true, |
| 55 | +AttachStderr:true, |
| 56 | +Env: []string{ |
| 57 | +fmt.Sprintf("GITHUB_PERSONAL_ACCESS_TOKEN=%s",os.Getenv("GITHUB_PERSONAL_ACCESS_TOKEN")), |
| 58 | +}, |
| 59 | +Image:image, |
| 60 | +} |
| 61 | + |
| 62 | +resp,err:=dockerClient.ContainerCreate( |
| 63 | +ctx, |
| 64 | +containerCfg, |
| 65 | +&container.HostConfig{}, |
| 66 | +&network.NetworkingConfig{}, |
| 67 | +nil, |
| 68 | +"") |
| 69 | +require.NoError(t,err) |
| 70 | + |
| 71 | +t.Cleanup(func() { |
| 72 | +require.NoError(t,dockerClient.ContainerRemove(ctx,resp.ID, container.RemoveOptions{Force:true})) |
| 73 | +}) |
| 74 | + |
| 75 | +hijackedResponse,err:=dockerClient.ContainerAttach(ctx,resp.ID, container.AttachOptions{ |
| 76 | +Stream:true, |
| 77 | +Stdin:true, |
| 78 | +Stdout:true, |
| 79 | +Stderr:true, |
| 80 | +}) |
| 81 | +require.NoError(t,err) |
| 82 | +t.Cleanup(func() {hijackedResponse.Close() }) |
| 83 | + |
| 84 | +require.NoError(t,dockerClient.ContainerStart(ctx,resp.ID, container.StartOptions{})) |
| 85 | + |
| 86 | +serverStart:=make(chanserverStartResult) |
| 87 | +gofunc() { |
| 88 | +prOut,pwOut:=io.Pipe() |
| 89 | +prErr,pwErr:=io.Pipe() |
| 90 | + |
| 91 | +gofunc() { |
| 92 | +// Ignore error, we should be done? |
| 93 | +// TODO: maybe check for use of closed network connection specifically |
| 94 | +_,_=stdcopy.StdCopy(pwOut,pwErr,hijackedResponse.Reader) |
| 95 | +pwOut.Close() |
| 96 | +pwErr.Close() |
| 97 | +}() |
| 98 | + |
| 99 | +bufferedStderr:=bufio.NewReader(prErr) |
| 100 | +line,err:=bufferedStderr.ReadString('\n') |
| 101 | +iferr!=nil { |
| 102 | +serverStart<-serverStartResult{err:err} |
| 103 | +} |
| 104 | + |
| 105 | +ifstrings.TrimSpace(line)!="GitHub MCP Server running on stdio" { |
| 106 | +serverStart<-serverStartResult{ |
| 107 | +err:fmt.Errorf("unexpected server output: %s",line), |
| 108 | +} |
| 109 | +return |
| 110 | +} |
| 111 | + |
| 112 | +serverStart<-serverStartResult{ |
| 113 | +server:server{ |
| 114 | +m:m, |
| 115 | +log:testLogWriter{t}, |
| 116 | +stdin:hijackedResponse.Conn, |
| 117 | +stdout:bufio.NewReader(prOut), |
| 118 | +}, |
| 119 | +} |
| 120 | +}() |
| 121 | + |
| 122 | +t.Logf("waiting for %s server to start...",m) |
| 123 | +serveResult:=<-serverStart |
| 124 | +require.NoError(t,serveResult.err,"expected the server to start successfully") |
| 125 | + |
| 126 | +returnserveResult.server |
| 127 | +} |
| 128 | + |
| 129 | +funcTestCapabilities(t*testing.T) { |
| 130 | +anthropicServer:=start(t,anthropic) |
| 131 | +githubServer:=start(t,github) |
| 132 | + |
| 133 | +req:=newInitializeRequest( |
| 134 | +initializeRequestParams{ |
| 135 | +ProtocolVersion:"2025-03-26", |
| 136 | +Capabilities:clientCapabilities{}, |
| 137 | +ClientInfo:clientInfo{ |
| 138 | +Name:"ConformanceTest", |
| 139 | +Version:"0.0.1", |
| 140 | +}, |
| 141 | +}, |
| 142 | +) |
| 143 | + |
| 144 | +require.NoError(t,anthropicServer.send(req)) |
| 145 | + |
| 146 | +varanthropicInitializeResponseinitializeResponse |
| 147 | +require.NoError(t,anthropicServer.receive(&anthropicInitializeResponse)) |
| 148 | + |
| 149 | +require.NoError(t,githubServer.send(req)) |
| 150 | + |
| 151 | +varghInitializeResponseinitializeResponse |
| 152 | +require.NoError(t,githubServer.receive(&ghInitializeResponse)) |
| 153 | + |
| 154 | +// Any capabilities in the anthropic response should be present in the github response |
| 155 | +// (though the github response may have additional capabilities) |
| 156 | +ifdiff:=diffNonNilFields(anthropicInitializeResponse.Result.Capabilities,ghInitializeResponse.Result.Capabilities,"");diff!="" { |
| 157 | +t.Errorf("capabilities mismatch:\n%s",diff) |
| 158 | +} |
| 159 | +} |
| 160 | + |
| 161 | +funcdiffNonNilFields(a,binterface{},pathstring)string { |
| 162 | +varsb strings.Builder |
| 163 | + |
| 164 | +va:=reflect.ValueOf(a) |
| 165 | +vb:=reflect.ValueOf(b) |
| 166 | + |
| 167 | +if!va.IsValid() { |
| 168 | +return"" |
| 169 | +} |
| 170 | + |
| 171 | +ifva.Kind()==reflect.Ptr { |
| 172 | +ifva.IsNil() { |
| 173 | +return"" |
| 174 | +} |
| 175 | +if!vb.IsValid()||vb.IsNil() { |
| 176 | +sb.WriteString(path+"\n") |
| 177 | +returnsb.String() |
| 178 | +} |
| 179 | +va=va.Elem() |
| 180 | +vb=vb.Elem() |
| 181 | +} |
| 182 | + |
| 183 | +ifva.Kind()!=reflect.Struct||vb.Kind()!=reflect.Struct { |
| 184 | +return"" |
| 185 | +} |
| 186 | + |
| 187 | +t:=va.Type() |
| 188 | +fori:=rangeva.NumField() { |
| 189 | +field:=t.Field(i) |
| 190 | +if!field.IsExported() { |
| 191 | +continue |
| 192 | +} |
| 193 | + |
| 194 | +subPath:=field.Name |
| 195 | +ifpath!="" { |
| 196 | +subPath=fmt.Sprintf("%s.%s",path,field.Name) |
| 197 | +} |
| 198 | + |
| 199 | +fieldA:=va.Field(i) |
| 200 | +fieldB:=vb.Field(i) |
| 201 | + |
| 202 | +switchfieldA.Kind() { |
| 203 | +casereflect.Ptr: |
| 204 | +iffieldA.IsNil() { |
| 205 | +continue// not required |
| 206 | +} |
| 207 | +iffieldB.IsNil() { |
| 208 | +sb.WriteString(subPath+"\n") |
| 209 | +continue |
| 210 | +} |
| 211 | +sb.WriteString(diffNonNilFields(fieldA.Interface(),fieldB.Interface(),subPath)) |
| 212 | + |
| 213 | +casereflect.Struct: |
| 214 | +sb.WriteString(diffNonNilFields(fieldA.Interface(),fieldB.Interface(),subPath)) |
| 215 | + |
| 216 | +default: |
| 217 | +zero:=reflect.Zero(fieldA.Type()) |
| 218 | +if!reflect.DeepEqual(fieldA.Interface(),zero.Interface()) { |
| 219 | +// fieldA is non-zero; now check that fieldB matches |
| 220 | +if!reflect.DeepEqual(fieldA.Interface(),fieldB.Interface()) { |
| 221 | +sb.WriteString(subPath+"\n") |
| 222 | +} |
| 223 | +} |
| 224 | +} |
| 225 | +} |
| 226 | + |
| 227 | +returnsb.String() |
| 228 | +} |
| 229 | + |
| 230 | +typeserverStartResultstruct { |
| 231 | +serverserver |
| 232 | +errerror |
| 233 | +} |
| 234 | + |
| 235 | +typeserverstruct { |
| 236 | +mmaintainer |
| 237 | +log io.Writer |
| 238 | + |
| 239 | +stdin io.Writer |
| 240 | +stdout*bufio.Reader |
| 241 | +} |
| 242 | + |
| 243 | +func (sserver)send(reqrequest)error { |
| 244 | +b,err:=req.marshal() |
| 245 | +iferr!=nil { |
| 246 | +returnerr |
| 247 | +} |
| 248 | + |
| 249 | +fmt.Fprintf(s.log,"sending %s: %s\n",s.m,string(b)) |
| 250 | + |
| 251 | +n,err:=s.stdin.Write(append(b,'\n')) |
| 252 | +iferr!=nil { |
| 253 | +returnerr |
| 254 | +} |
| 255 | + |
| 256 | +ifn!=len(b)+1 { |
| 257 | +returnfmt.Errorf("wrote %d bytes, expected %d",n,len(b)+1) |
| 258 | +} |
| 259 | + |
| 260 | +returnnil |
| 261 | +} |
| 262 | + |
| 263 | +func (sserver)receive(resresponse)error { |
| 264 | +line,err:=s.stdout.ReadBytes('\n') |
| 265 | +iferr!=nil { |
| 266 | +iferr==io.EOF { |
| 267 | +returnfmt.Errorf("EOF after reading %s",string(line)) |
| 268 | +} |
| 269 | +returnerr |
| 270 | +} |
| 271 | + |
| 272 | +fmt.Fprintf(s.log,"received from %s: %s\n",s.m,string(line)) |
| 273 | + |
| 274 | +returnres.unmarshal(line) |
| 275 | +} |
| 276 | + |
| 277 | +typejsonRPRCRequest[paramsany]struct { |
| 278 | +JSONRPCstring`json:"jsonrpc"` |
| 279 | +IDint`json:"id"` |
| 280 | +Methodstring`json:"method"` |
| 281 | +Paramsparams`json:"params"` |
| 282 | +} |
| 283 | + |
| 284 | +typejsonRPRCResponse[resultany]struct { |
| 285 | +JSONRPCstring`json:"jsonrpc"` |
| 286 | +IDint`json:"id"` |
| 287 | +Methodstring`json:"method"` |
| 288 | +Resultresult`json:"result"` |
| 289 | +} |
| 290 | + |
| 291 | +typerequestinterface { |
| 292 | +marshal() ([]byte,error) |
| 293 | +} |
| 294 | + |
| 295 | +typeresponseinterface { |
| 296 | +unmarshal([]byte)error |
| 297 | +} |
| 298 | + |
| 299 | +funcnewInitializeRequest(paramsinitializeRequestParams)initializeRequest { |
| 300 | +returninitializeRequest{ |
| 301 | +jsonRPRCRequest:jsonRPRCRequest[initializeRequestParams]{ |
| 302 | +JSONRPC:"2.0", |
| 303 | +ID:1, |
| 304 | +Method:"initialize", |
| 305 | +Params:params, |
| 306 | +}, |
| 307 | +} |
| 308 | +} |
| 309 | + |
| 310 | +typeinitializeRequeststruct { |
| 311 | +jsonRPRCRequest[initializeRequestParams] |
| 312 | +} |
| 313 | + |
| 314 | +func (rinitializeRequest)marshal() ([]byte,error) { |
| 315 | +returnjson.Marshal(r) |
| 316 | +} |
| 317 | + |
| 318 | +typeinitializeRequestParamsstruct { |
| 319 | +ProtocolVersionstring`json:"protocolVersion"` |
| 320 | +CapabilitiesclientCapabilities`json:"capabilities"` |
| 321 | +ClientInfoclientInfo`json:"clientInfo"` |
| 322 | +} |
| 323 | + |
| 324 | +typeclientCapabilitiesstruct{}// don't actually care about any of these right now |
| 325 | + |
| 326 | +typeclientInfostruct { |
| 327 | +Namestring`json:"name"` |
| 328 | +Versionstring`json:"version"` |
| 329 | +} |
| 330 | + |
| 331 | +typeinitializeResponsestruct { |
| 332 | +jsonRPRCResponse[initializeResult] |
| 333 | +} |
| 334 | + |
| 335 | +func (r*initializeResponse)unmarshal(b []byte)error { |
| 336 | +returnjson.Unmarshal(b,r) |
| 337 | +} |
| 338 | + |
| 339 | +typeinitializeResultstruct { |
| 340 | +ProtocolVersionstring`json:"protocolVersion"` |
| 341 | +CapabilitiesserverCapabilities`json:"capabilities"` |
| 342 | +ServerInfoserverInfo`json:"serverInfo"` |
| 343 | +} |
| 344 | + |
| 345 | +typeserverCapabilitiesstruct { |
| 346 | +Logging*struct{}`json:"logging,omitempty"` |
| 347 | +Prompts*struct { |
| 348 | +ListChangedbool`json:"listChanged,omitempty"` |
| 349 | +}`json:"prompts,omitempty"` |
| 350 | +Resources*struct { |
| 351 | +Subscribebool`json:"subscribe,omitempty"` |
| 352 | +ListChangedbool`json:"listChanged,omitempty"` |
| 353 | +}`json:"resources,omitempty"` |
| 354 | +Tools*struct { |
| 355 | +ListChangedbool`json:"listChanged,omitempty"` |
| 356 | +}`json:"tools,omitempty"` |
| 357 | +} |
| 358 | + |
| 359 | +typeserverInfostruct { |
| 360 | +Namestring`json:"name"` |
| 361 | +Versionstring`json:"version"` |
| 362 | +} |