Deletes the hand-written internal/tools/mcp client and transports. The provider talks to an SDK ClientSession over CommandTransport, StreamableClientTransport or SSEClientTransport, and reconnects when the server exits. The http transport now keeps a real streamable session instead of stateless POSTs. Sampling goes through one adapter, since the SDK marks it deprecated (SEP-2577). message_endpoint is removed: the SSE transport takes the POST URL from the server's endpoint event.
60 lines
2.5 KiB
Go
60 lines
2.5 KiB
Go
package taskmcp
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/ai-sdk/chat"
|
|
|
|
"github.com/samcharles93/archie-core/internal/agentexec/modelloop"
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
"github.com/samcharles93/archie-core/internal/domain/agentrun"
|
|
protocol "github.com/samcharles93/archie-core/internal/tools/provider/mcp"
|
|
)
|
|
|
|
func TestSamplingKeepsTaskCredentialsAndUsage(t *testing.T) {
|
|
t.Setenv("ARCHIE_SAMPLING_FIXTURE_KEY", "instance-key")
|
|
for _, key := range []string{"org-a-key", "org-b-key"} {
|
|
t.Run(key, func(t *testing.T) {
|
|
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Header.Get("Authorization") != "Bearer "+key {
|
|
t.Error("sampling used another org's key")
|
|
}
|
|
var input map[string]any
|
|
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if input["model"] != "task-model" {
|
|
t.Errorf("sampling model %v", input["model"])
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"id":"sample","object":"chat.completion","model":"task-model","choices":[{"index":0,"message":{"role":"assistant","content":"sampled"},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":2,"total_tokens":5}}`))
|
|
}))
|
|
defer upstream.Close()
|
|
providers := map[string]agentrun.Provider{"fixture": {Class: "openai", BaseURL: upstream.URL + "/v1", APIKeyEnv: "ARCHIE_SAMPLING_FIXTURE_KEY"}}
|
|
llm := modelloop.NewRuntimeWithKeys(providers, nil, map[string]string{"fixture": key})
|
|
recorded := false
|
|
sample := SamplingHandler(llm, config.TaskConfig{Models: map[string]string{"default": "fixture/task-model"}}, func(_ context.Context, model string, usage chat.Usage) {
|
|
recorded = true
|
|
if model != "fixture/task-model" || usage.TotalTokens != 5 {
|
|
t.Errorf("usage attribution %s %+v", model, usage)
|
|
}
|
|
})
|
|
result, err := sample(t.Context(), protocol.SamplingRequest{Messages: []protocol.SamplingMessage{{Role: "user", Content: protocol.SamplingContent{Type: "text", Text: "sample"}}}, MaxTokens: 10})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if os.Getenv("ARCHIE_SAMPLING_FIXTURE_KEY") != "instance-key" {
|
|
t.Fatal("sampling changed process-wide credentials")
|
|
}
|
|
if !recorded || result.Model != "fixture/task-model" || result.Text != "sampled" {
|
|
t.Fatalf("sampling result %+v, recorded %v", result, recorded)
|
|
}
|
|
})
|
|
}
|
|
}
|