MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / TestTokenInfo

Function TestTokenInfo

mcp/streamable_client_test.go:1108–1158  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1106}
1107
1108func TestTokenInfo(t *testing.T) {
1109 ctx := context.Background()
1110
1111 // Create a server with a tool that returns TokenInfo.
1112 tokenInfo := func(ctx context.Context, req *CallToolRequest, _ struct{}) (*CallToolResult, any, error) {
1113 return &CallToolResult{Content: []Content{&TextContent{Text: fmt.Sprintf("%v", req.Extra.TokenInfo)}}}, nil, nil
1114 }
1115 server := NewServer(testImpl, nil)
1116 AddTool(server, &Tool{Name: "tokenInfo", Description: "return token info"}, tokenInfo)
1117
1118 streamHandler := NewStreamableHTTPHandler(func(req *http.Request) *Server { return server }, nil)
1119 verifier := func(ctx context.Context, token string, req *http.Request) (*auth.TokenInfo, error) {
1120 if token != "test-token" {
1121 return nil, auth.ErrInvalidToken
1122 }
1123 return &auth.TokenInfo{
1124 Scopes: []string{"scope"},
1125 // Expiration is far, far in the future.
1126 Expiration: time.Date(5000, 1, 2, 3, 4, 5, 0, time.UTC),
1127 }, nil
1128 }
1129 handler := auth.RequireBearerToken(verifier, nil)(streamHandler)
1130 httpServer := httptest.NewServer(mustNotPanic(t, handler))
1131 defer httpServer.Close()
1132
1133 transport := &StreamableClientTransport{
1134 Endpoint: httpServer.URL,
1135 OAuthHandler: &mockOAuthHandler{token: &oauth2.Token{AccessToken: "test-token"}},
1136 }
1137 client := NewClient(testImpl, nil)
1138 session, err := client.Connect(ctx, transport, nil)
1139 if err != nil {
1140 t.Fatalf("client.Connect() failed: %v", err)
1141 }
1142 defer session.Close()
1143
1144 res, err := session.CallTool(ctx, &CallToolParams{Name: "tokenInfo"})
1145 if err != nil {
1146 t.Fatal(err)
1147 }
1148 if len(res.Content) == 0 {
1149 t.Fatal("missing content")
1150 }
1151 tc, ok := res.Content[0].(*TextContent)
1152 if !ok {
1153 t.Fatal("not TextContent")
1154 }
1155 if g, w := tc.Text, "&{[scope] 5000-01-02 03:04:05 +0000 UTC map[]}"; g != w {
1156 t.Errorf("got %q, want %q", g, w)
1157 }
1158}

Callers

nothing calls this directly

Calls 9

ConnectMethod · 0.95
RequireBearerTokenFunction · 0.92
NewServerFunction · 0.85
AddToolFunction · 0.85
NewStreamableHTTPHandlerFunction · 0.85
mustNotPanicFunction · 0.85
NewClientFunction · 0.85
CallToolMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…