Files
wangdefa f782b612ec build: 内置 norma 模块,构建不再依赖上游仓库
把 github.com/Autumn-27/norma v0.4.3(commit 320ae32)的源码复制到
third_party/norma,并用 go.mod 的 replace 指向本地副本。上游删除仓库或
改写 tag 都不再影响本仓库构建。

- 已验证 GOPROXY=off 下可完整编译并打出发行包
- go.sum 中该模块的两条校验记录随之删除(本地 replace 不需要哈希)
- 升级方法与授权注意事项见 third_party/README.md
2026-10-20 21:10:41 -07:00

87 lines
2.3 KiB
Go

package mcp
import (
"bufio"
"context"
"encoding/json"
"io"
"strings"
"testing"
)
func TestInProcessServerNamingAndCall(t *testing.T) {
tools := InProcessServer("calc", []InProcessTool{{
Name: "add",
Description: "add",
Schema: map[string]any{"type": "object"},
Handler: func(_ context.Context, args json.RawMessage) (string, error) {
var a struct{ A, B float64 }
json.Unmarshal(args, &a)
return "sum", nil
},
}})
if len(tools) != 1 || tools[0].Name() != "mcp__calc__add" {
t.Fatalf("naming wrong: %v", tools[0].Name())
}
res, _ := tools[0].Call(context.Background(), []byte(`{"a":1,"b":2}`), nil)
if res.Flatten() != "sum" {
t.Fatalf("call result: %q", res.Flatten())
}
}
// fakeServer speaks just enough MCP JSON-RPC to satisfy initialize, tools/list,
// and tools/call, over the provided pipes.
func fakeServer(in io.Reader, out io.Writer) {
sc := bufio.NewScanner(in)
enc := json.NewEncoder(out)
for sc.Scan() {
var req map[string]any
if json.Unmarshal(sc.Bytes(), &req) != nil {
continue
}
id, hasID := req["id"]
method, _ := req["method"].(string)
if !hasID {
continue // notification
}
var result any
switch method {
case "initialize":
result = map[string]any{"protocolVersion": "2024-11-05"}
case "tools/list":
result = map[string]any{"tools": []any{map[string]any{
"name": "echo", "description": "echoes", "inputSchema": map[string]any{"type": "object"},
}}}
case "tools/call":
result = map[string]any{"content": []any{map[string]any{"type": "text", "text": "echoed!"}}}
}
enc.Encode(map[string]any{"jsonrpc": "2.0", "id": id, "result": result})
}
}
func TestStdioClientOverPipes(t *testing.T) {
cReader, sWriter := io.Pipe() // server -> client
sReader, cWriter := io.Pipe() // client -> server
go fakeServer(sReader, sWriter)
ctx := context.Background()
client, err := newClientWithConn(ctx, "srv", cReader, cWriter, nil)
if err != nil {
t.Fatalf("init: %v", err)
}
tools, err := client.Tools(ctx)
if err != nil {
t.Fatalf("tools: %v", err)
}
if len(tools) != 1 || tools[0].Name() != "mcp__srv__echo" {
t.Fatalf("tool naming: %+v", tools)
}
res, err := tools[0].Call(ctx, []byte(`{"msg":"hi"}`), nil)
if err != nil {
t.Fatalf("call: %v", err)
}
if !strings.Contains(res.Flatten(), "echoed!") {
t.Fatalf("call result: %q", res.Flatten())
}
}