Files
ai_academy/internal/mcp/client_tools_test.go
runny 9058149576
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

185 lines
6.1 KiB
Go

package mcp
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"testing"
"github.com/mark3labs/mcp-go/client"
"github.com/mark3labs/mcp-go/client/transport"
sdkmcp "github.com/mark3labs/mcp-go/mcp"
"github.com/stretchr/testify/require"
)
type rawToolsTransport struct {
transport.Interface
send func(context.Context, transport.JSONRPCRequest) (*transport.JSONRPCResponse, error)
}
func (t *rawToolsTransport) SendRequest(
ctx context.Context,
request transport.JSONRPCRequest,
) (*transport.JSONRPCResponse, error) {
return t.send(ctx, request)
}
func TestRawToolsPaginationErrorsAndCancellation(t *testing.T) {
for _, second := range []string{`{"tools":[],"nextCursor":"repeat"}`, `{"error":true}`, `not-json`} {
t.Run(second, func(t *testing.T) {
calls := 0
seenIDs := map[string]bool{}
tpt := &rawToolsTransport{
send: func(_ context.Context, request transport.JSONRPCRequest) (*transport.JSONRPCResponse, error) {
calls++
id, err := json.Marshal(request.ID)
require.NoError(t, err)
require.Contains(t, string(id), `"academy-tools-`)
require.False(t, seenIDs[string(id)])
seenIDs[string(id)] = true
require.Equal(t, "tools/list", request.Method)
if calls == 1 {
return &transport.JSONRPCResponse{Result: json.RawMessage(`{
"tools": [
{
"name": "first",
"inputSchema": {
"type": "object"
}
}
],
"nextCursor": "repeat"
}`)}, nil
}
params, _ := json.Marshal(request.Params)
require.JSONEq(t, `{"cursor":"repeat"}`, string(params))
if second == `{"error":true}` {
return &transport.JSONRPCResponse{
Error: &sdkmcp.JSONRPCErrorDetails{Code: -32603, Message: "page unavailable"},
}, nil
}
return &transport.JSONRPCResponse{Result: json.RawMessage(second)}, nil
},
}
c := &mcpGoClient{client: client.NewClient(tpt)}
c.toolSchemas = map[string]json.RawMessage{"previous": json.RawMessage(`{"type":"object"}`)}
c.initialized.Store(true)
tools, err := c.ListTools(context.Background())
require.Error(t, err)
require.Nil(t, tools, "never publish a partial directory")
require.Contains(t, c.toolSchemas, "previous", "a failed refresh keeps the last complete header schema")
require.NotContains(t, c.toolSchemas, "first", "partial pages must not become call metadata")
require.Equal(t, 2, calls)
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err = c.ListTools(ctx)
require.ErrorIs(t, err, context.Canceled)
require.Equal(t, 2, calls)
})
}
}
func TestRawToolsBoundsHostileDirectorySize(t *testing.T) {
// Distinct cursors defeat the repeat-cursor guard, so an unbounded reader
// would keep growing the in-memory directory until the list timeout.
t.Run("pages", func(t *testing.T) {
calls := 0
c := &mcpGoClient{client: client.NewClient(&rawToolsTransport{
send: func(context.Context, transport.JSONRPCRequest) (*transport.JSONRPCResponse, error) {
calls++
return &transport.JSONRPCResponse{Result: json.RawMessage(
fmt.Sprintf(`{"tools":[{"name":"t%d"}],"nextCursor":"c%d"}`, calls, calls),
)}, nil
},
})}
c.initialized.Store(true)
tools, err := c.ListTools(context.Background())
require.ErrorContains(t, err, "exceeded")
require.Nil(t, tools, "never publish a partial directory")
require.LessOrEqual(t, calls, maxToolListPages)
})
t.Run("tools", func(t *testing.T) {
page := make([]string, maxToolsPerService+1)
for i := range page {
page[i] = fmt.Sprintf(`{"name":"t%d"}`, i)
}
body := `{"tools":[` + strings.Join(page, ",") + `]}`
c := &mcpGoClient{client: client.NewClient(&rawToolsTransport{
send: func(context.Context, transport.JSONRPCRequest) (*transport.JSONRPCResponse, error) {
return &transport.JSONRPCResponse{Result: json.RawMessage(body)}, nil
},
})}
c.initialized.Store(true)
_, err := c.ListTools(context.Background())
require.ErrorContains(t, err, "exceeded")
})
t.Run("schema", func(t *testing.T) {
schema := `{"type":"object","description":"` + strings.Repeat("x", maxToolSchemaBytes) + `"}`
c := &mcpGoClient{client: client.NewClient(&rawToolsTransport{
send: func(context.Context, transport.JSONRPCRequest) (*transport.JSONRPCResponse, error) {
return &transport.JSONRPCResponse{Result: json.RawMessage(
`{"tools":[{"name":"huge","inputSchema":` + schema + `}]}`,
)}, nil
},
})}
c.initialized.Store(true)
_, err := c.ListTools(context.Background())
require.ErrorContains(t, err, "exceeds")
})
t.Run("within limits", func(t *testing.T) {
c := &mcpGoClient{client: client.NewClient(&rawToolsTransport{
send: func(context.Context, transport.JSONRPCRequest) (*transport.JSONRPCResponse, error) {
return &transport.JSONRPCResponse{Result: json.RawMessage(
`{"tools":[{"name":"ok","inputSchema":{"type":"object"}}]}`,
)}, nil
},
})}
c.initialized.Store(true)
tools, err := c.ListTools(context.Background())
require.NoError(t, err)
require.Len(t, tools, 1)
})
}
func TestRawToolsUsesOAuthLifecycleAndPreservesTransportErrors(t *testing.T) {
runtime, _, refreshes, closeServer := newOAuthLifecycleFixture(
t,
http.StatusOK,
map[string]any{
"access_token": "new-access",
"refresh_token": "rotated-refresh",
"token_type": "Bearer",
"expires_in": 3600,
},
)
defer closeServer()
c := &mcpGoClient{
oauth: runtime,
client: client.NewClient(
&rawToolsTransport{
send: func(context.Context, transport.JSONRPCRequest) (*transport.JSONRPCResponse, error) {
require.EqualValues(t, 1, refreshes.Load(), "refresh must precede discovery")
return &transport.JSONRPCResponse{Result: json.RawMessage(`{"tools":[]}`)}, nil
},
},
),
}
c.initialized.Store(true)
_, err := c.ListTools(context.Background())
require.NoError(t, err)
c.oauth = nil
sentinel := errors.New("network failure")
c.client = client.NewClient(
&rawToolsTransport{send: func(context.Context, transport.JSONRPCRequest) (*transport.JSONRPCResponse, error) {
return nil, sentinel
}},
)
_, err = c.ListTools(context.Background())
require.ErrorIs(t, err, sentinel)
var transportErr *transport.Error
require.ErrorAs(t, err, &transportErr)
}