Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5134931260 | |||
| 5724f0a94b | |||
| e071084ebe |
@@ -24,14 +24,18 @@ rewrites.
|
||||
|
||||
### Capability — the headline (roadmap: Now / Next)
|
||||
|
||||
1. **Agent spend observability** ([#4787](https://github.com/micro/go-micro/issues/4787)) — surface x402 spend in `RunInfo` and OpenTelemetry so payments are inspectable like every other agent action. (Follows #4786.)
|
||||
2. **Example: an agent that pays for a paid tool** ([#4788](https://github.com/micro/go-micro/issues/4788)) — the runnable artifact that makes it real for a developer, against a mock facilitator (no live funds). (Follows #4786/#4787.)
|
||||
3. **gRPC-reflection MCP** ([#4796](https://github.com/micro/go-micro/issues/4796)) — expose external reflected gRPC services as MCP tools, not only go-micro-native handlers. A large jump in what agents can operate without requiring teams to rewrite existing services.
|
||||
4. **Kubernetes operator + CRDs foundation** ([#4797](https://github.com/micro/go-micro/issues/4797)) — add the first opt-in `Agent`, `Service`, and `Flow` resource foundation so the services → agents → workflows lifecycle has a native deployment path for Kubernetes users.
|
||||
1. **A2A external-client conformance** ([#4815](https://github.com/micro/go-micro/issues/4815)) — make the gateway easier for non-go-micro agents to discover and stream from by serving the well-known agent card path and spec SSE events.
|
||||
2. **AP2 mandate foundation for agent payments** ([#4841](https://github.com/micro/go-micro/issues/4841)) — add opt-in checkout/payment mandate signing and verification so A2A-carried payment authority can settle over x402 without changing defaults.
|
||||
3. **Kubernetes CRD reconciler foundation** ([#4842](https://github.com/micro/go-micro/issues/4842)) — turn the shipped alpha `Agent`, `Service`, and `Flow` CRDs into a minimally runnable native deployment path with workload reconciliation and status conditions.
|
||||
|
||||
### In flight — do not re-queue
|
||||
|
||||
_None right now._
|
||||
|
||||
### Background — hardening & DX (roadmap: Ongoing; capped)
|
||||
|
||||
_Background hardening is intentionally empty right now. Recent work covered first-agent
|
||||
wayfinding, plan/delegate recovery, provider fallback repair, streaming, memory
|
||||
compaction, retry controls, and provider-failure inspection. Further churn in those
|
||||
compaction, retry controls, provider-failure inspection, x402 buyer safety, gRPC-reflection MCP,
|
||||
MCP result conformance, and the alpha Kubernetes CRD surface. Further churn in those
|
||||
areas should be marked `needs-human` unless it unlocks a clear user-visible capability._
|
||||
|
||||
@@ -14,8 +14,11 @@ name: "Loop: Builder"
|
||||
|
||||
on:
|
||||
workflow_dispatch: {}
|
||||
schedule:
|
||||
- cron: "29 * * * *"
|
||||
# PAUSED 2026-07-12: automatic schedule disabled while the team does focused
|
||||
# 1:1 fixes. Still runnable on demand via workflow_dispatch. Re-enable by
|
||||
# uncommenting the schedule below.
|
||||
# schedule:
|
||||
# - cron: "29 * * * *"
|
||||
|
||||
permissions:
|
||||
issues: write
|
||||
|
||||
@@ -14,8 +14,11 @@ name: "Loop: Coherence"
|
||||
|
||||
on:
|
||||
workflow_dispatch: {}
|
||||
schedule:
|
||||
- cron: "0 7 * * *"
|
||||
# PAUSED 2026-07-12: automatic schedule disabled while the team does focused
|
||||
# 1:1 fixes. Still runnable on demand via workflow_dispatch. Re-enable by
|
||||
# uncommenting the schedule below.
|
||||
# schedule:
|
||||
# - cron: "0 7 * * *"
|
||||
|
||||
permissions:
|
||||
issues: write
|
||||
|
||||
@@ -14,8 +14,11 @@ name: "Loop: Planner"
|
||||
|
||||
on:
|
||||
workflow_dispatch: {}
|
||||
schedule:
|
||||
- cron: "59 * * * *"
|
||||
# PAUSED 2026-07-12: automatic schedule disabled while the team does focused
|
||||
# 1:1 fixes. Still runnable on demand via workflow_dispatch. Re-enable by
|
||||
# uncommenting the schedule below.
|
||||
# schedule:
|
||||
# - cron: "59 * * * *"
|
||||
|
||||
permissions:
|
||||
issues: write
|
||||
|
||||
@@ -12,8 +12,11 @@ name: "Loop: Release"
|
||||
|
||||
on:
|
||||
workflow_dispatch: {}
|
||||
schedule:
|
||||
- cron: "0 23 * * *"
|
||||
# PAUSED 2026-07-12: automatic nightly release disabled while the team does
|
||||
# focused 1:1 fixes. Cut a release on demand via workflow_dispatch. Re-enable
|
||||
# by uncommenting the schedule below.
|
||||
# schedule:
|
||||
# - cron: "0 23 * * *"
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
@@ -14,8 +14,11 @@ name: "Loop: Security"
|
||||
|
||||
on:
|
||||
workflow_dispatch: {}
|
||||
schedule:
|
||||
- cron: "0 6 * * 1"
|
||||
# PAUSED 2026-07-12: automatic schedule disabled while the team does focused
|
||||
# 1:1 fixes. Still runnable on demand via workflow_dispatch. Re-enable by
|
||||
# uncommenting the schedule below.
|
||||
# schedule:
|
||||
# - cron: "0 6 * * 1"
|
||||
|
||||
permissions:
|
||||
issues: write
|
||||
|
||||
@@ -7,9 +7,13 @@ name: "Loop: Triage"
|
||||
# failures become fixes with no human in the middle. Gated on CODEX_TRIGGER_TOKEN.
|
||||
|
||||
on:
|
||||
workflow_run:
|
||||
workflows: ["Harness (E2E)", "Lint", "Run Tests", "govulncheck"]
|
||||
types: [completed]
|
||||
workflow_dispatch: {}
|
||||
# PAUSED 2026-07-12: automatic CI-failure dispatch disabled while the team
|
||||
# does focused 1:1 fixes, so failures don't auto-spawn agent tasks. Re-enable
|
||||
# by uncommenting the workflow_run trigger below.
|
||||
# workflow_run:
|
||||
# workflows: ["Harness (E2E)", "Lint", "Run Tests", "govulncheck"]
|
||||
# types: [completed]
|
||||
|
||||
permissions:
|
||||
issues: write
|
||||
|
||||
@@ -25,10 +25,14 @@ below is kept current between tags and rolled into the next version when it ship
|
||||
|
||||
### Changed
|
||||
- **Remote agent chat streaming** — `micro chat` now streams replies from remote agents instead of waiting for the full response. (`cmd/micro/`, `agent/`)
|
||||
- **A2A external-client conformance** — the A2A gateway now serves the Agent Card at the spec 0.3.0 `/.well-known/agent-card.json` (keeping `/.well-known/agent.json` as a legacy alias), and `message/stream` emits spec-shaped `status-update`/`artifact-update` events ending in a `final:true` status-update instead of repeated full `Task` snapshots — and never sends `result` and `error` together. Standard A2A clients (ADK, LangGraph, a2a-SDK) can now discover and stream from go-micro agents. (`gateway/a2a/`)
|
||||
|
||||
### Fixed
|
||||
- **Provider failure inspection metadata** — provider failures recorded during agent runs now retain classification metadata for inspection. (`agent/`, `ai/`)
|
||||
|
||||
### Security
|
||||
- **x402 spend-cap hardening** — the paying `Client` now refuses a 402 whose `maxAmountRequired` is not a positive integer (a swallowed parse error or negative amount previously bypassed the budget cap), and a new `Config.RequireSettlement` fails closed when a paid request is served by a verify-only facilitator that never captures funds. (`wrapper/x402/`)
|
||||
|
||||
---
|
||||
|
||||
## [6.7.0] - July 2026
|
||||
|
||||
+514
@@ -0,0 +1,514 @@
|
||||
# Go Micro [](https://pkg.go.dev/go-micro.dev/v6?tab=doc) [](https://discord.gg/G8Gk5j3uXr)
|
||||
|
||||
Go Micro is an **agent harness** and service framework for Go.
|
||||
|
||||
**Community:** questions, ideas, or just want to build alongside us? [Join the Discord](https://discord.gg/G8Gk5j3uXr).
|
||||
|
||||
A harness is the runtime around an agent: the tools it can call, the memory it keeps, the guardrails that bound it, the workflows that trigger it, the services it depends on, and the protocols other agents use to reach it.
|
||||
|
||||
Go Micro gives you the harness as Go code. Build an agent and it gets a model, memory, tools, planning, delegation, guardrails, and service discovery; it is reachable over [MCP](https://modelcontextprotocol.io/) and [A2A](https://a2a-protocol.org). Write services and every endpoint becomes an AI-callable tool. Orchestrate the deterministic parts with durable flows. Agents, services, and flows share one runtime because an agent is a distributed system, and building one is building a service.
|
||||
|
||||
## Sponsors
|
||||
|
||||
<a href="https://go-micro.dev/blog/3"><img src="https://upload.wikimedia.org/wikipedia/commons/7/78/Anthropic_logo.svg" height="26" /></a>
|
||||
|
||||
<a href="https://go-micro.dev/blog/29"><img src="https://upload.wikimedia.org/wikipedia/commons/4/4d/OpenAI_Logo.svg" height="26" /></a>
|
||||
|
||||
<a href="https://go-micro.dev/blog/8"><img src="https://www.atlascloud.ai/logo.svg" height="26" /></a>
|
||||
|
||||
**Want to support Go Micro and see your logo here?** [Become a sponsor](https://discord.gg/G8Gk5j3uXr) — reach out on Discord.
|
||||
|
||||
## Commercial Support
|
||||
|
||||
Running Go Micro in production, or building on it and want help? Paid **support, consulting, training, and retainers** are available directly from the maintainer — and they're what keep the project maintained. See [**Support**](SUPPORT.md) for the tiers, or [open a request](https://github.com/micro/go-micro/issues/new?template=commercial_support.md).
|
||||
|
||||
## Contents
|
||||
|
||||
- [Quick Start](#quick-start)
|
||||
- [First agent on-ramp](#first-agent-on-ramp)
|
||||
- [Why an Agent Harness](#why-an-agent-harness)
|
||||
- [Writing Services](#writing-services)
|
||||
- [Building Agents](#building-agents) — [Plan & Delegate](#plan--delegate), [Pluggable](#batteries-included-pluggable), [Paid tools (x402)](#paid-tools-x402), [A2A](#reachable-by-other-agents-a2a)
|
||||
- [Features](#features)
|
||||
- [CLI](#cli)
|
||||
- [Autonomous improvement loop](#autonomous-improvement-loop)
|
||||
- [Multi-Service Projects](#multi-service-projects)
|
||||
- [Data Model](#data-model)
|
||||
- [AI Providers](#ai-providers)
|
||||
- [Examples](#examples)
|
||||
- [Commercial Support](#commercial-support)
|
||||
- [Docs](#docs)
|
||||
|
||||
## Quick Start
|
||||
|
||||
Install the CLI:
|
||||
|
||||
```bash
|
||||
# Binary (no Go required)
|
||||
curl -fsSL https://go-micro.dev/install.sh | sh
|
||||
|
||||
# Or with Go
|
||||
go install go-micro.dev/v6/cmd/micro@latest
|
||||
```
|
||||
|
||||
If install or `PATH` checks fail, use the [install troubleshooting guide](internal/website/docs/guides/install-troubleshooting.md) before scaffolding your first service.
|
||||
|
||||
### Fastest start — no API key
|
||||
|
||||
Scaffold a service, run it, call it:
|
||||
|
||||
```bash
|
||||
micro new helloworld
|
||||
cd helloworld
|
||||
micro run
|
||||
```
|
||||
|
||||
Then in another terminal:
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:8080/api/helloworld/Helloworld.Call \
|
||||
-H 'Content-Type: application/json' -d '{"name":"World"}'
|
||||
```
|
||||
|
||||
This install → scaffold → run → call path is covered by no-secret CI harnesses. To
|
||||
verify just the local installer and first-run CLI boundaries without network
|
||||
access or provider keys, use:
|
||||
|
||||
```bash
|
||||
make install-smoke
|
||||
```
|
||||
|
||||
To verify the focused CLI inner-loop contract — scaffold → run/chat/inspect → deploy dry-run — use:
|
||||
|
||||
```bash
|
||||
make inner-loop
|
||||
```
|
||||
|
||||
To run only the ordered [0→hero services → agents → workflows transcript](internal/website/docs/guides/zero-to-hero.md) that CI guards, use:
|
||||
|
||||
```bash
|
||||
make zero-to-hero-transcript
|
||||
```
|
||||
|
||||
To run the broader local contract (including that transcript, chat/inspect CLI boundaries, and deploy dry-run), use:
|
||||
|
||||
```bash
|
||||
make harness
|
||||
```
|
||||
|
||||
### First agent on-ramp
|
||||
|
||||
After install and the first `micro new`/`micro run` smoke check, take the
|
||||
walkable agent path in this order:
|
||||
|
||||
1. [Install troubleshooting](internal/website/docs/guides/install-troubleshooting.md) — verify the binary installer or `go install`, `PATH`, `micro --version`, and the no-secret smoke path before agent work.
|
||||
|
||||
Run `make docs-wayfinding` to verify the focused no-secret docs/CLI contract that keeps these README and website commands aligned with the installed CLI.
|
||||
|
||||
2. `micro agent demo` — print the provider-free first-agent demo command and next docs steps from the installed CLI.
|
||||
3. `micro agent quickcheck` (or `micro agent debug`) — when scaffold → run → chat → inspect stalls, print the short recovery map before you dive into the full debugging guide.
|
||||
4. `micro examples` — print the maintained provider-free runnable examples in copy/paste order.
|
||||
5. `micro zero-to-hero` — print the maintained one-command no-secret lifecycle harness and runnable examples.
|
||||
6. [Examples wayfinding index](examples/INDEX.md) — choose the smallest no-secret first-agent, maintained [0→hero support reference](examples/support/), and next interop examples from one map.
|
||||
7. [Smallest first-agent example](examples/first-agent/) — run one service-backed agent with a mock model and no provider key.
|
||||
8. [No-secret first-agent transcript](internal/website/docs/guides/no-secret-first-agent.md) — run the
|
||||
maintained support agent with a mock model and see services → agents → workflows succeed without a key.
|
||||
9. [Your First Agent](internal/website/docs/guides/your-first-agent.md) — build a
|
||||
service-backed agent and talk to it with `micro chat`.
|
||||
10. [Debugging your agent](internal/website/docs/guides/debugging-agents.md) — use
|
||||
`micro agent preflight` before `micro run`, `micro agent doctor` after `micro run`,
|
||||
then `micro chat` and `micro inspect agent <name>` to recover run history, memory,
|
||||
and provider checks when the first conversation does something unexpected.
|
||||
11. [0→hero Reference](internal/website/docs/guides/zero-to-hero.md) — complete the
|
||||
services → agents → workflows loop with scaffold, run, chat, inspect, flow
|
||||
history, and deploy dry-run commands that match the maintained harness.
|
||||
|
||||
### Autonomous improvement loop
|
||||
|
||||
Want the same services → agents → workflows lifecycle applied to your
|
||||
repository? `micro loop` scaffolds the autonomous improvement loop used by Go
|
||||
Micro itself: a North Star, ranked issue queue, role prompts, GitHub Actions
|
||||
workflows, and verification for CI-gated PRs.
|
||||
|
||||
```bash
|
||||
micro loop init --roles all
|
||||
micro loop verify
|
||||
```
|
||||
|
||||
Before turning on the schedule, configure a dispatch token such as
|
||||
`CODEX_TRIGGER_TOKEN`, protect the default branch with required CI checks
|
||||
(`go build ./...`, `go test ./...`, and `golangci-lint run ./...` for this
|
||||
repository), and seed `.github/loop/PRIORITIES.md` with one scoped issue per
|
||||
increment. See the [`micro loop` quickstart](internal/website/docs/guides/micro-loop.md)
|
||||
for the setup checklist and operating model.
|
||||
|
||||
### Generate from a prompt — with an LLM key
|
||||
|
||||
Set a provider key, describe what you want, and the AI designs services, writes handlers, compiles, and starts them:
|
||||
|
||||
```bash
|
||||
export ANTHROPIC_API_KEY=sk-ant-... # or OPENAI_API_KEY, GEMINI_API_KEY, ...
|
||||
micro run --prompt "a task management system with categories" --provider anthropic
|
||||
```
|
||||
|
||||
The AI designs the architecture, you review it, then it generates handlers with real business logic, compiles them, and starts them:
|
||||
|
||||
```
|
||||
Services:
|
||||
● task — Task management with status tracking
|
||||
● project — Project organization
|
||||
|
||||
Generate? [Y/n]
|
||||
|
||||
Micro
|
||||
Services:
|
||||
● task
|
||||
● project
|
||||
Agents:
|
||||
◆ agent
|
||||
```
|
||||
|
||||
Then talk to your services from the console:
|
||||
|
||||
```
|
||||
> Create a project called Launch, then add three tasks to it
|
||||
|
||||
→ project_Project_Create({"name":"Launch"})
|
||||
← {"record":{"id":"p1..."},"success":true}
|
||||
→ task_Task_Create({"title":"Design specs","project_id":"p1..."})
|
||||
→ task_Task_Create({"title":"Write code","project_id":"p1..."})
|
||||
→ task_Task_Create({"title":"Ship it","project_id":"p1..."})
|
||||
|
||||
Created project Launch and added three tasks to it.
|
||||
```
|
||||
|
||||
When you need a capability that doesn't exist, the agent generates a new service mid-conversation:
|
||||
|
||||
```
|
||||
> I need to track shipping. Create a shipment for order 123 to London.
|
||||
|
||||
⚡ generating shipping service...
|
||||
✓ shipping
|
||||
→ shipping_Shipping_Create({"order_id":"123","destination":"London"})
|
||||
← {"record":{"id":"xyz...","status":"pending"}}
|
||||
|
||||
Created shipment for order 123 going to London.
|
||||
```
|
||||
|
||||
Edit the generated code by hand at any time — re-running preserves your changes. [Read more](https://go-micro.dev/blog/13).
|
||||
|
||||
## Why an Agent Harness
|
||||
|
||||
The first wave of agent frameworks helped developers put a model in a loop. The next problem is operating that loop: connecting it to real tools, scoping what it can touch, preserving state, routing work to specialists, recovering from failures, observing what happened, and letting other agents call it. That is harness work.
|
||||
|
||||
Go Micro's answer is to make the harness the same thing you already deploy:
|
||||
|
||||
- **Tools are services** — endpoint metadata becomes tool schema; RPC executes the call.
|
||||
- **Agents are services** — they register, discover, load-balance, and expose `Agent.Chat`.
|
||||
- **Workflows are durable code paths** — use flows when the path is known; dispatch to agents when it is not.
|
||||
- **Safety lives at execution** — `MaxSteps`, `LoopLimit`, `ApproveTool`, and tool wrappers run where actions happen.
|
||||
- **Interop is built in** — MCP for tools, A2A for agents, x402 for paid tools.
|
||||
|
||||
Use Go Micro when the agent has to operate a system, not just answer a prompt.
|
||||
|
||||
## Writing Services
|
||||
|
||||
Under the hood, a service is a struct with methods. Doc comments and `@example` tags become tool descriptions for AI agents automatically.
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go-micro.dev/v6"
|
||||
)
|
||||
|
||||
type Request struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type Say struct{}
|
||||
|
||||
// Hello greets a person by name.
|
||||
// @example {"name": "Alice"}
|
||||
func (h *Say) Hello(ctx context.Context, req *Request, rsp *Response) error {
|
||||
rsp.Message = "Hello " + req.Name
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
service := micro.NewService("greeter")
|
||||
service.Handle(new(Say))
|
||||
service.Run()
|
||||
}
|
||||
```
|
||||
|
||||
Run it and everything is accessible — REST, gRPC, MCP, agent playground:
|
||||
|
||||
```bash
|
||||
micro run
|
||||
# Dashboard: http://localhost:8080
|
||||
# API: http://localhost:8080/api/{service}/{method}
|
||||
# Agent: http://localhost:8080/agent
|
||||
# MCP Tools: http://localhost:8080/mcp/tools
|
||||
```
|
||||
|
||||
You can also scaffold a service from a template:
|
||||
|
||||
```bash
|
||||
micro new helloworld
|
||||
micro new contacts --template crud
|
||||
```
|
||||
|
||||
## Building Agents
|
||||
|
||||
An Agent is a service with an LLM inside it. It has a proto-defined `Agent.Chat` RPC endpoint, registers in the registry, and is callable like any service:
|
||||
|
||||
```go
|
||||
agent := micro.NewAgent("task-mgr",
|
||||
micro.AgentServices("task", "project"),
|
||||
micro.AgentPrompt("You manage tasks and projects. You understand deadlines and priorities."),
|
||||
micro.AgentProvider("anthropic"),
|
||||
)
|
||||
agent.Run()
|
||||
```
|
||||
|
||||
The agent discovers its services from the registry, scopes its tools to their endpoints, and maintains conversation memory in the store. It registers itself so `micro chat` and other agents can find it.
|
||||
|
||||
```go
|
||||
// Programmatic interaction
|
||||
resp, _ := agent.Ask(ctx, "What tasks are overdue?")
|
||||
fmt.Println(resp.Reply)
|
||||
```
|
||||
|
||||
Multiple agents coordinate via RPC — each is a service with an `Agent.Chat` endpoint. `micro chat` routes to the right one.
|
||||
|
||||
```bash
|
||||
micro agent list # list registered agents
|
||||
micro call task-mgr Agent.Chat '{"message": "What tasks are overdue?"}'
|
||||
```
|
||||
|
||||
### Plan & Delegate
|
||||
|
||||
Every agent gets two built-in harness capabilities, exposed as tools — no extra setup or separate graph runtime:
|
||||
|
||||
- **`plan`** — for multi-step work, the agent records an ordered plan in its store-backed memory and stays oriented across turns.
|
||||
- **`delegate`** — the agent hands a self-contained subtask to another agent. If a registered agent already owns the relevant services, the hand-off goes over RPC to that agent; otherwise a focused, short-lived sub-agent is created for the subtask with its own isolated context.
|
||||
|
||||
This keeps intelligence distributed: an agent doesn't need to know *how* to do everything, only *who* does. See [examples/agent-plan-delegate](examples/agent-plan-delegate/).
|
||||
|
||||
```go
|
||||
// A sub-agent is just an agent — created with New, talked to with Ask.
|
||||
// delegate-first: reuse a registered agent, or spin up a focused one.
|
||||
resp, _ := agent.Ask(ctx, "Plan the launch, create the tasks, and have comms notify the owner.")
|
||||
```
|
||||
|
||||
### Batteries included, pluggable
|
||||
|
||||
Just as a service composes pluggable abstractions (registry, broker, store), an agent composes a **model**, **memory**, and **tools** — sane defaults out of the box, each swappable.
|
||||
|
||||
```go
|
||||
agent := micro.NewAgent("assistant",
|
||||
micro.AgentProvider("anthropic"), // model — swap the provider
|
||||
micro.AgentCompactMemory(40, 12), // memory — durable, summarized, recallable
|
||||
micro.AgentTool("weather", "Get the weather for a city",
|
||||
map[string]any{"city": map[string]any{"type": "string"}},
|
||||
func(ctx context.Context, in map[string]any) (string, error) {
|
||||
return getWeather(in["city"].(string)) // tools beyond your services — any function
|
||||
}),
|
||||
micro.AgentMaxSteps(8), // guardrails
|
||||
)
|
||||
```
|
||||
|
||||
**Memory** is durable and store-backed by default (Postgres, NATS KV, or file), so an agent picks up where it left off after a restart — or supply your own with `AgentMemory`. Long-running agents can opt into `AgentCompactMemory(maxMessages, keepRecent)`: older turns are collapsed into a deterministic summary, recent turns stay verbatim, and relevant archived turns are recalled on future asks without replaying the whole conversation. **Tools** are your services automatically, plus any function you register with `AgentTool`.
|
||||
|
||||
### Paid tools (x402)
|
||||
|
||||
Every endpoint is an AI-callable tool — and it can be a *paid* tool. Go Micro supports [x402](https://x402.org), the HTTP 402 payment standard for agents, so a tool can require a stablecoin payment and an agent can settle it autonomously. It's opt-in and carries no crypto in the framework: verification is delegated to a pluggable facilitator (Coinbase, Alchemy, self-hosted), so Base and Solana are just different facilitators.
|
||||
|
||||
```bash
|
||||
# Charge for tool calls at the MCP gateway (off unless you set a pay-to address)
|
||||
micro mcp serve --x402_pay_to 0xYourAddress --x402_network solana --x402_amount 10000
|
||||
# Per-tool amounts via a config file
|
||||
micro mcp serve --x402_config x402.json
|
||||
```
|
||||
|
||||
See the [Payments (x402) guide](internal/website/docs/guides/x402-payments.md).
|
||||
|
||||
### Reachable by other agents (A2A)
|
||||
|
||||
Within a Go Micro system, agents reach each other over RPC. To make them reachable by agents on *other* frameworks, Go Micro speaks the [Agent2Agent (A2A) protocol](https://a2a-protocol.org). The A2A gateway discovers your agents from the registry, generates an Agent Card for each from its metadata — the same way the MCP gateway derives tools from service endpoints — and translates incoming A2A tasks to the agent's `Agent.Chat` RPC. No per-agent code: register an agent and it's reachable over A2A.
|
||||
|
||||
```bash
|
||||
micro a2a serve --address :4000 # gateway: expose every registered agent over A2A
|
||||
micro a2a list # agents and their Agent Card URLs
|
||||
```
|
||||
|
||||
Or skip the gateway entirely — an agent can serve its own A2A endpoint directly, handling tasks in-process:
|
||||
|
||||
```go
|
||||
micro.NewAgent("task-mgr", micro.AgentServices("task"), micro.AgentA2A(":4000"))
|
||||
```
|
||||
|
||||
It works both ways. To call an agent on another framework, an `a2a.Client` is wired into the two places that hand off work: `flow.A2A(url)` as a workflow step (the cross-framework `Dispatch`), and `delegate` to an `http(s)` URL from inside an agent.
|
||||
|
||||
MCP exposes your services as tools; A2A exposes your agents as agents. See the [A2A guide](internal/website/docs/guides/a2a-protocol.md).
|
||||
|
||||
## Features
|
||||
|
||||
### AI
|
||||
|
||||
| Feature | Details |
|
||||
|---------|---------|
|
||||
| Agents | `micro.NewAgent()` — intelligent layer that manages services |
|
||||
| Plan & delegate | Built-in agent tools — plan multi-step work, delegate subtasks to other agents |
|
||||
| Pluggable memory | Durable store-backed conversation memory by default; swap with `AgentMemory` |
|
||||
| Custom tools | `AgentTool` — give an agent any function as a tool, beyond its services |
|
||||
| Guardrails | `MaxSteps` (stop on count), `LoopLimit` (stop repeated no-progress calls), `ApproveTool` (human-in-the-loop) |
|
||||
| Tool middleware | `AgentWrapTool` — wrap tool execution for logging, metrics, or retries (like client/server wrappers) |
|
||||
| Workflows | `micro.NewFlow()` — event-driven; one step, ordered durable steps, or triggers an agent |
|
||||
| Durable execution | Checkpointed flow steps survive a crash and resume where they stopped; store-backed by default, pluggable backend |
|
||||
| MCP gateway | Every endpoint is an AI tool automatically |
|
||||
| A2A gateway | Every agent is reachable over the Agent2Agent protocol; cards generated from the registry (`micro a2a`) |
|
||||
| Payments (x402) | Opt-in per-call payments for tools via the x402 standard; pluggable facilitator (Base, Solana, …) |
|
||||
| 9 LLM providers | Anthropic, OpenAI, Gemini, Groq, Mistral, Together, Atlas Cloud, MiniMax, Ollama (local + cloud) |
|
||||
| Interactive console | `micro run` includes a chat console for talking to services |
|
||||
| Service generation | `micro run --prompt` — describe a system, get running services |
|
||||
|
||||
### Framework
|
||||
|
||||
| Feature | Details |
|
||||
|---------|---------|
|
||||
| Service registry | mDNS (default), Consul, etcd |
|
||||
| RPC client/server | gRPC transport, load balancing, streaming |
|
||||
| Pub/sub events | NATS, RabbitMQ, HTTP broker |
|
||||
| Key-value store | File (bbolt), Postgres, NATS KV |
|
||||
| Typed model layer | CRUD + queries, SQLite/Postgres backends |
|
||||
| Everything swappable | All abstractions are Go interfaces |
|
||||
|
||||
### Developer experience & deployment
|
||||
|
||||
| Feature | Details |
|
||||
|---------|---------|
|
||||
| Hot reload | `micro run` watches files, rebuilds on change |
|
||||
| Templates | `micro new --template crud/pubsub/api` |
|
||||
| One-command deploy | `micro deploy user@server` — SSH + systemd, no Docker |
|
||||
|
||||
## CLI
|
||||
|
||||
| Command | Purpose |
|
||||
|---------|---------|
|
||||
| `micro run --prompt "..."` | Generate services + agent, start with interactive console |
|
||||
| `micro run` | Dev mode: hot reload, gateway, interactive console |
|
||||
| `micro run -d` | Detached mode (no console) |
|
||||
| `micro chat` | Standalone chat (when not using micro run) |
|
||||
| `micro agent list` | List registered agents |
|
||||
| `micro new myservice` | Scaffold a service |
|
||||
| `micro call service endpoint '{}'` | Call a service or agent from the CLI |
|
||||
| `micro build` | Compile production binaries |
|
||||
| `micro deploy user@server` | Deploy via SSH + systemd |
|
||||
|
||||
## Multi-Service Projects
|
||||
|
||||
Run multiple services together:
|
||||
|
||||
```go
|
||||
users := micro.NewService("users", micro.Address(":9001"))
|
||||
orders := micro.NewService("orders", micro.Address(":9002"))
|
||||
|
||||
users.Handle(new(Users))
|
||||
orders.Handle(new(Orders))
|
||||
|
||||
g := micro.NewGroup(users, orders)
|
||||
g.Run()
|
||||
```
|
||||
|
||||
Or use a `micro.mu` config file:
|
||||
|
||||
```
|
||||
service users
|
||||
path ./users
|
||||
|
||||
service orders
|
||||
path ./orders
|
||||
depends users
|
||||
```
|
||||
|
||||
## Data Model
|
||||
|
||||
Typed persistence with CRUD and queries:
|
||||
|
||||
```go
|
||||
type User struct {
|
||||
ID string `json:"id" model:"key"`
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email" model:"index"`
|
||||
}
|
||||
|
||||
db := service.Model()
|
||||
db.Register(&User{})
|
||||
db.Create(ctx, &User{ID: "1", Name: "Alice", Email: "alice@example.com"})
|
||||
|
||||
var results []*User
|
||||
db.List(ctx, &results, model.Where("email", "alice@example.com"))
|
||||
```
|
||||
|
||||
Backends: memory (default), SQLite, Postgres.
|
||||
|
||||
## AI Providers
|
||||
|
||||
Swap providers with a single import — same interface everywhere:
|
||||
|
||||
| Provider | Default Model |
|
||||
|----------|---------------|
|
||||
| Anthropic | `claude-sonnet-4-20250514` |
|
||||
| OpenAI | `gpt-4o` |
|
||||
| Google Gemini | `gemini-2.5-flash` |
|
||||
| Groq | `llama-3.3-70b-versatile` |
|
||||
| Mistral | `mistral-large-latest` |
|
||||
| Together AI | `meta-llama/Llama-3.3-70B-Instruct-Turbo` |
|
||||
| Atlas Cloud | `deepseek-ai/DeepSeek-V3-0324` |
|
||||
| MiniMax | `MiniMax-M3` |
|
||||
| Ollama | `llama3.2` (local) |
|
||||
|
||||
```go
|
||||
m := ai.New("anthropic", ai.WithAPIKey(key))
|
||||
resp, _ := m.Generate(ctx, &ai.Request{Prompt: "hello"})
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
New to agents? Follow the [first-agent on-ramp](#first-agent-on-ramp), then use the [examples index](examples/README.md) for the full services → agents → workflows map.
|
||||
|
||||
- [hello-world](examples/hello-world/) — Basic RPC service
|
||||
- [multi-service](examples/multi-service/) — Multiple services in one binary
|
||||
- [mcp](examples/mcp/) — MCP integration with AI agents
|
||||
- [first-agent](examples/first-agent/) — Smallest provider-free service-backed agent
|
||||
- [agent-plan-delegate](examples/agent-plan-delegate/) — Agent planning and multi-agent delegation
|
||||
- [agent-durable](examples/agent-durable/) — Checkpoint and resume an agent run without replaying completed tool side effects
|
||||
- [grpc-interop](examples/grpc-interop/) — Call go-micro from any gRPC client
|
||||
|
||||
See [all examples](examples/README.md).
|
||||
|
||||
## Docs
|
||||
|
||||
- [Getting Started](internal/website/docs/getting-started.md)
|
||||
- [AI Integration](internal/website/docs/ai-integration.md)
|
||||
- [Your First Agent](internal/website/docs/guides/your-first-agent.md)
|
||||
- [0→hero Reference](internal/website/docs/guides/zero-to-hero.md)
|
||||
- [Agents and Workflows](internal/website/docs/guides/agents-and-workflows.md)
|
||||
- [Agent Design](internal/docs/AGENT_DESIGN.md)
|
||||
- [Plan & Delegate](internal/website/docs/guides/plan-delegate.md)
|
||||
- [Agent Guardrails](internal/website/docs/guides/agent-guardrails.md)
|
||||
- [Payments (x402)](internal/website/docs/guides/x402-payments.md)
|
||||
- [MCP & AI Agents](internal/website/docs/mcp.md)
|
||||
- [Data Model](internal/website/docs/model.md)
|
||||
- [Deployment](internal/website/docs/deployment.md)
|
||||
- [Plugins](internal/website/docs/plugins.md)
|
||||
|
||||
Package reference: https://pkg.go.dev/go-micro.dev/v6
|
||||
@@ -1,14 +1,20 @@
|
||||
<!-- WEHUB_ZH_README -->
|
||||
> [!NOTE]
|
||||
> 本文档由 WeHub 基于上游 README 翻译整理,属于社区翻译,非官方中文文档。
|
||||
> [English](./README.en.md) · [原始项目](https://github.com/micro/go-micro) · [上游 README](https://github.com/micro/go-micro/blob/HEAD/README.md)
|
||||
> 原作者、版权与许可证归属以原始项目及本仓库 LICENSE 文件为准。
|
||||
|
||||
# Go Micro [](https://pkg.go.dev/go-micro.dev/v6?tab=doc) [](https://discord.gg/G8Gk5j3uXr)
|
||||
|
||||
Go Micro is an **agent harness** and service framework for Go.
|
||||
Go Micro 是一个面向 Go 的 **agent harness(智能体运行时框架)** 与服务框架。
|
||||
|
||||
**Community:** questions, ideas, or just want to build alongside us? [Join the Discord](https://discord.gg/G8Gk5j3uXr).
|
||||
**社区:** 有问题、有想法,或想与我们一起构建?[加入 Discord](https://discord.gg/G8Gk5j3uXr).
|
||||
|
||||
A harness is the runtime around an agent: the tools it can call, the memory it keeps, the guardrails that bound it, the workflows that trigger it, the services it depends on, and the protocols other agents use to reach it.
|
||||
Harness 是围绕智能体的运行时:它能调用的工具、它保留的记忆、约束它的护栏、触发它的工作流、它依赖的服务,以及其他智能体与之通信所使用的协议。
|
||||
|
||||
Go Micro gives you the harness as Go code. Build an agent and it gets a model, memory, tools, planning, delegation, guardrails, and service discovery; it is reachable over [MCP](https://modelcontextprotocol.io/) and [A2A](https://a2a-protocol.org). Write services and every endpoint becomes an AI-callable tool. Orchestrate the deterministic parts with durable flows. Agents, services, and flows share one runtime because an agent is a distributed system, and building one is building a service.
|
||||
Go Micro 以 Go 代码的形式提供该 harness。构建一个智能体即可获得模型、记忆、工具、规划、委派、护栏与服务发现;可通过 [MCP](https://modelcontextprotocol.io/) 和 [A2A](https://a2a-protocol.org). 被访问。编写服务后,每个端点都会成为 AI 可调用的工具。用持久化 flow 编排确定性部分。智能体、服务与 flow 共享同一运行时,因为智能体本质上是一个分布式系统,构建智能体就是在构建服务。
|
||||
|
||||
## Sponsors
|
||||
## 赞助商
|
||||
|
||||
<a href="https://go-micro.dev/blog/3"><img src="https://upload.wikimedia.org/wikipedia/commons/7/78/Anthropic_logo.svg" height="26" /></a>
|
||||
|
||||
@@ -16,32 +22,32 @@ Go Micro gives you the harness as Go code. Build an agent and it gets a model, m
|
||||
|
||||
<a href="https://go-micro.dev/blog/8"><img src="https://www.atlascloud.ai/logo.svg" height="26" /></a>
|
||||
|
||||
**Want to support Go Micro and see your logo here?** [Become a sponsor](https://discord.gg/G8Gk5j3uXr) — reach out on Discord.
|
||||
**想支持 Go Micro 并在此展示你的 logo?** [成为赞助商](https://discord.gg/G8Gk5j3uXr) — 可通过 Discord 联系。
|
||||
|
||||
## Commercial Support
|
||||
## 商业支持
|
||||
|
||||
Running Go Micro in production, or building on it and want help? Paid **support, consulting, training, and retainers** are available directly from the maintainer — and they're what keep the project maintained. See [**Support**](SUPPORT.md) for the tiers, or [open a request](https://github.com/micro/go-micro/issues/new?template=commercial_support.md).
|
||||
要在生产环境运行 Go Micro,或基于它进行开发并需要帮助?可直接向维护者购买 **支持、咨询、培训与 retainer(长期服务)** — 这些也是项目得以持续维护的支撑。详见 [**Support**](SUPPORT.md) 中的层级说明,或 [提交请求](https://github.com/micro/go-micro/issues/new?template=commercial_support.md).
|
||||
|
||||
## Contents
|
||||
## 目录
|
||||
|
||||
- [Quick Start](#quick-start)
|
||||
- [First agent on-ramp](#first-agent-on-ramp)
|
||||
- [Why an Agent Harness](#why-an-agent-harness)
|
||||
- [Writing Services](#writing-services)
|
||||
- [Building Agents](#building-agents) — [Plan & Delegate](#plan--delegate), [Pluggable](#batteries-included-pluggable), [Paid tools (x402)](#paid-tools-x402), [A2A](#reachable-by-other-agents-a2a)
|
||||
- [Features](#features)
|
||||
- [快速开始](#quick-start)
|
||||
- [首个智能体入门路径](#first-agent-on-ramp)
|
||||
- [为何需要 Agent Harness](#why-an-agent-harness)
|
||||
- [编写服务](#writing-services)
|
||||
- [构建智能体](#building-agents) — [规划与委派](#plan--delegate)、[可插拔](#batteries-included-pluggable)、[付费工具 (x402)](#paid-tools-x402)、[A2A](#reachable-by-other-agents-a2a)
|
||||
- [功能特性](#features)
|
||||
- [CLI](#cli)
|
||||
- [Autonomous improvement loop](#autonomous-improvement-loop)
|
||||
- [Multi-Service Projects](#multi-service-projects)
|
||||
- [Data Model](#data-model)
|
||||
- [AI Providers](#ai-providers)
|
||||
- [Examples](#examples)
|
||||
- [Commercial Support](#commercial-support)
|
||||
- [Docs](#docs)
|
||||
- [自主改进循环](#autonomous-improvement-loop)
|
||||
- [多服务项目](#multi-service-projects)
|
||||
- [数据模型](#data-model)
|
||||
- [AI 提供商](#ai-providers)
|
||||
- [示例](#examples)
|
||||
- [商业支持](#commercial-support)
|
||||
- [文档](#docs)
|
||||
|
||||
## Quick Start
|
||||
## 快速开始
|
||||
|
||||
Install the CLI:
|
||||
安装 CLI:
|
||||
|
||||
```bash
|
||||
# Binary (no Go required)
|
||||
@@ -51,11 +57,11 @@ curl -fsSL https://go-micro.dev/install.sh | sh
|
||||
go install go-micro.dev/v6/cmd/micro@latest
|
||||
```
|
||||
|
||||
If install or `PATH` checks fail, use the [install troubleshooting guide](internal/website/docs/guides/install-troubleshooting.md) before scaffolding your first service.
|
||||
如果安装或 `PATH` 检查失败,在搭建第一个服务之前,请先参阅 [安装故障排除指南](internal/website/docs/guides/install-troubleshooting.md)。
|
||||
|
||||
### Fastest start — no API key
|
||||
### 最快上手 — 无需 API key
|
||||
|
||||
Scaffold a service, run it, call it:
|
||||
搭建服务、运行并调用:
|
||||
|
||||
```bash
|
||||
micro new helloworld
|
||||
@@ -63,95 +69,85 @@ cd helloworld
|
||||
micro run
|
||||
```
|
||||
|
||||
Then in another terminal:
|
||||
然后在另一个终端中:
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:8080/api/helloworld/Helloworld.Call \
|
||||
-H 'Content-Type: application/json' -d '{"name":"World"}'
|
||||
```
|
||||
|
||||
This install → scaffold → run → call path is covered by no-secret CI harnesses. To
|
||||
verify just the local installer and first-run CLI boundaries without network
|
||||
access or provider keys, use:
|
||||
这条 安装 → 搭建 → 运行 → 调用 路径由无密钥 CI harness 覆盖。若要在无网络访问或提供商密钥的情况下,仅验证本地安装器与首次运行 CLI 边界,请使用:
|
||||
|
||||
```bash
|
||||
make install-smoke
|
||||
```
|
||||
|
||||
To verify the focused CLI inner-loop contract — scaffold → run/chat/inspect → deploy dry-run — use:
|
||||
若要验证聚焦的 CLI 内循环契约 — 搭建 → 运行/聊天/检查 → 部署 dry-run — 请使用:
|
||||
|
||||
```bash
|
||||
make inner-loop
|
||||
```
|
||||
|
||||
To run only the ordered [0→hero services → agents → workflows transcript](internal/website/docs/guides/zero-to-hero.md) that CI guards, use:
|
||||
若仅运行 CI 守护的有序 [0→hero 服务 → 智能体 → 工作流 脚本](internal/website/docs/guides/zero-to-hero.md),请使用:
|
||||
|
||||
```bash
|
||||
make zero-to-hero-transcript
|
||||
```
|
||||
|
||||
To run the broader local contract (including that transcript, chat/inspect CLI boundaries, and deploy dry-run), use:
|
||||
若要运行更广泛的本地契约(包括该脚本、聊天/检查 CLI 边界以及部署 dry-run),请使用:
|
||||
|
||||
```bash
|
||||
make harness
|
||||
```
|
||||
|
||||
### First agent on-ramp
|
||||
### 首个智能体入门路径
|
||||
|
||||
After install and the first `micro new`/`micro run` smoke check, take the
|
||||
walkable agent path in this order:
|
||||
完成安装并进行首次 `micro new`/`micro run` 冒烟检查后,按以下顺序走可逐步跟进的智能体路径:
|
||||
|
||||
1. [Install troubleshooting](internal/website/docs/guides/install-troubleshooting.md) — verify the binary installer or `go install`, `PATH`, `micro --version`, and the no-secret smoke path before agent work.
|
||||
1. [安装故障排除](internal/website/docs/guides/install-troubleshooting.md) — 在开始智能体相关工作前,验证二进制安装器或 `go install`、`PATH`、`micro --version`,以及无密钥冒烟路径。
|
||||
|
||||
Run `make docs-wayfinding` to verify the focused no-secret docs/CLI contract that keeps these README and website commands aligned with the installed CLI.
|
||||
运行 `make docs-wayfinding` 以验证聚焦的无密钥 docs/CLI 契约,确保本 README 与网站命令与已安装的 CLI 保持一致。
|
||||
|
||||
2. `micro agent demo` — print the provider-free first-agent demo command and next docs steps from the installed CLI.
|
||||
3. `micro agent quickcheck` (or `micro agent debug`) — when scaffold → run → chat → inspect stalls, print the short recovery map before you dive into the full debugging guide.
|
||||
4. `micro examples` — print the maintained provider-free runnable examples in copy/paste order.
|
||||
5. `micro zero-to-hero` — print the maintained one-command no-secret lifecycle harness and runnable examples.
|
||||
6. [Examples wayfinding index](examples/INDEX.md) — choose the smallest no-secret first-agent, maintained [0→hero support reference](examples/support/), and next interop examples from one map.
|
||||
7. [Smallest first-agent example](examples/first-agent/) — run one service-backed agent with a mock model and no provider key.
|
||||
8. [No-secret first-agent transcript](internal/website/docs/guides/no-secret-first-agent.md) — run the
|
||||
maintained support agent with a mock model and see services → agents → workflows succeed without a key.
|
||||
9. [Your First Agent](internal/website/docs/guides/your-first-agent.md) — build a
|
||||
service-backed agent and talk to it with `micro chat`.
|
||||
10. [Debugging your agent](internal/website/docs/guides/debugging-agents.md) — use
|
||||
`micro agent preflight` before `micro run`, `micro agent doctor` after `micro run`,
|
||||
then `micro chat` and `micro inspect agent <name>` to recover run history, memory,
|
||||
and provider checks when the first conversation does something unexpected.
|
||||
11. [0→hero Reference](internal/website/docs/guides/zero-to-hero.md) — complete the
|
||||
services → agents → workflows loop with scaffold, run, chat, inspect, flow
|
||||
history, and deploy dry-run commands that match the maintained harness.
|
||||
2. `micro agent demo` — 从已安装的 CLI 打印无需提供商的首个智能体演示命令及后续文档步骤。
|
||||
3. `micro agent quickcheck`(或 `micro agent debug`)— 当 搭建 → 运行 → 聊天 → 检查 流程卡住时,在深入完整调试指南前,先打印简短恢复路线图。
|
||||
4. `micro examples` — 按可复制/粘贴顺序打印维护中的、无需提供商的可运行示例。
|
||||
5. `micro zero-to-hero` — 打印维护中的一条命令无密钥生命周期 harness 与可运行示例。
|
||||
6. [示例导航索引](examples/INDEX.md) — 从一张地图中选择最小的无密钥首个智能体示例、维护中的 [0→hero 支持参考](examples/support/),以及后续互操作示例。
|
||||
7. [最小首个智能体示例](examples/first-agent/) — 使用 mock 模型且无需提供商密钥,运行一个由服务支撑的智能体。
|
||||
8. [无密钥首个智能体脚本](internal/website/docs/guides/no-secret-first-agent.md) — 使用 mock 模型运行维护中的支持智能体,在无密钥情况下见证 服务 → 智能体 → 工作流 成功。
|
||||
9. [你的第一个智能体](internal/website/docs/guides/your-first-agent.md) — 构建一个由服务支撑的智能体,并通过 `micro chat` 与之对话。
|
||||
10. [调试你的智能体](internal/website/docs/guides/debugging-agents.md) — 在 `micro run` 之前使用 `micro agent preflight`,在 `micro run` 之后使用 `micro agent doctor`,
|
||||
然后使用 `micro chat` 和 `micro inspect agent <name>` 恢复运行历史、记忆
|
||||
以及提供商检查,以应对首次对话出现意外行为的情况。
|
||||
11. [0→hero 参考](internal/website/docs/guides/zero-to-hero.md) — 通过与服务维护 harness 一致的 搭建、运行、聊天、检查、flow
|
||||
历史与部署 dry-run 命令,完成 服务 → 智能体 → 工作流 循环。
|
||||
|
||||
### Autonomous improvement loop
|
||||
### 自主改进循环
|
||||
|
||||
Want the same services → agents → workflows lifecycle applied to your
|
||||
repository? `micro loop` scaffolds the autonomous improvement loop used by Go
|
||||
Micro itself: a North Star, ranked issue queue, role prompts, GitHub Actions
|
||||
workflows, and verification for CI-gated PRs.
|
||||
想将同样的 服务 → 智能体 → 工作流 生命周期应用到你的仓库?`micro loop` 会搭建 Go
|
||||
Micro 自身使用的自主改进循环:North Star、排序后的问题队列、角色提示词、GitHub Actions
|
||||
工作流,以及面向 CI 门禁 PR 的验证。
|
||||
|
||||
```bash
|
||||
micro loop init --roles all
|
||||
micro loop verify
|
||||
```
|
||||
|
||||
Before turning on the schedule, configure a dispatch token such as
|
||||
`CODEX_TRIGGER_TOKEN`, protect the default branch with required CI checks
|
||||
(`go build ./...`, `go test ./...`, and `golangci-lint run ./...` for this
|
||||
repository), and seed `.github/loop/PRIORITIES.md` with one scoped issue per
|
||||
increment. See the [`micro loop` quickstart](internal/website/docs/guides/micro-loop.md)
|
||||
for the setup checklist and operating model.
|
||||
在启用定时任务之前,请配置 dispatch token,例如
|
||||
`CODEX_TRIGGER_TOKEN`,用必需的 CI 检查保护默认分支
|
||||
(对本仓库为 `go build ./...`、`go test ./...` 和 `golangci-lint run ./...`),
|
||||
并在每个增量中为 `.github/loop/PRIORITIES.md` 填入一个范围明确的问题。设置清单与运行模型请参阅 [`micro loop` 快速开始](internal/website/docs/guides/micro-loop.md)。
|
||||
|
||||
### Generate from a prompt — with an LLM key
|
||||
### 通过提示词生成 — 需要 LLM key
|
||||
|
||||
Set a provider key, describe what you want, and the AI designs services, writes handlers, compiles, and starts them:
|
||||
设置提供商密钥,描述你的需求,AI 会设计服务、编写 handler、编译并启动它们:
|
||||
|
||||
```bash
|
||||
export ANTHROPIC_API_KEY=sk-ant-... # or OPENAI_API_KEY, GEMINI_API_KEY, ...
|
||||
micro run --prompt "a task management system with categories" --provider anthropic
|
||||
```
|
||||
|
||||
The AI designs the architecture, you review it, then it generates handlers with real business logic, compiles them, and starts them:
|
||||
AI 设计架构,你进行评审,随后它会生成包含真实业务逻辑的 handler、编译并启动:
|
||||
|
||||
```
|
||||
Services:
|
||||
@@ -168,7 +164,7 @@ Micro
|
||||
◆ agent
|
||||
```
|
||||
|
||||
Then talk to your services from the console:
|
||||
然后在控制台中与你的服务对话:
|
||||
|
||||
```
|
||||
> Create a project called Launch, then add three tasks to it
|
||||
@@ -182,7 +178,7 @@ Then talk to your services from the console:
|
||||
Created project Launch and added three tasks to it.
|
||||
```
|
||||
|
||||
When you need a capability that doesn't exist, the agent generates a new service mid-conversation:
|
||||
当你需要尚不存在的能力时,代理会在对话过程中生成一项新服务:
|
||||
|
||||
```
|
||||
> I need to track shipping. Create a shipment for order 123 to London.
|
||||
@@ -195,25 +191,25 @@ When you need a capability that doesn't exist, the agent generates a new service
|
||||
Created shipment for order 123 going to London.
|
||||
```
|
||||
|
||||
Edit the generated code by hand at any time — re-running preserves your changes. [Read more](https://go-micro.dev/blog/13).
|
||||
你可以随时手动编辑生成的代码——重新运行会保留你的修改。[了解更多](https://go-micro.dev/blog/13).
|
||||
|
||||
## Why an Agent Harness
|
||||
## 为何需要 Agent Harness
|
||||
|
||||
The first wave of agent frameworks helped developers put a model in a loop. The next problem is operating that loop: connecting it to real tools, scoping what it can touch, preserving state, routing work to specialists, recovering from failures, observing what happened, and letting other agents call it. That is harness work.
|
||||
第一波代理框架帮助开发者让模型进入循环。下一个问题是如何运营这个循环:将其连接到真实工具、限定可触及范围、保留状态、将工作路由给专家、从故障中恢复、观测发生了什么,并让其他代理调用它。这就是 harness 工作。
|
||||
|
||||
Go Micro's answer is to make the harness the same thing you already deploy:
|
||||
Go Micro 的答案是:让 harness 与你已部署的东西合二为一:
|
||||
|
||||
- **Tools are services** — endpoint metadata becomes tool schema; RPC executes the call.
|
||||
- **Agents are services** — they register, discover, load-balance, and expose `Agent.Chat`.
|
||||
- **Workflows are durable code paths** — use flows when the path is known; dispatch to agents when it is not.
|
||||
- **Safety lives at execution** — `MaxSteps`, `LoopLimit`, `ApproveTool`, and tool wrappers run where actions happen.
|
||||
- **Interop is built in** — MCP for tools, A2A for agents, x402 for paid tools.
|
||||
- **工具即服务** — 端点元数据成为工具 schema;RPC 执行调用。
|
||||
- **代理即服务** — 它们注册、发现、负载均衡,并暴露 `Agent.Chat`。
|
||||
- **工作流即持久化代码路径** — 路径已知时使用 flows;未知时分派给代理。
|
||||
- **安全落在执行层** — `MaxSteps`、`LoopLimit`、`ApproveTool` 以及工具包装器在动作发生处运行。
|
||||
- **互操作内置** — 工具用 MCP,代理用 A2A,付费工具用 x402。
|
||||
|
||||
Use Go Micro when the agent has to operate a system, not just answer a prompt.
|
||||
当代理需要操作系统而不仅是回答提示时,使用 Go Micro。
|
||||
|
||||
## Writing Services
|
||||
## 编写服务
|
||||
|
||||
Under the hood, a service is a struct with methods. Doc comments and `@example` tags become tool descriptions for AI agents automatically.
|
||||
底层而言,服务是带方法的 struct。文档注释和 `@example` 标签会自动成为 AI 代理的工具描述。
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -248,7 +244,7 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
Run it and everything is accessible — REST, gRPC, MCP, agent playground:
|
||||
运行后一切皆可访问——REST、gRPC、MCP、agent playground:
|
||||
|
||||
```bash
|
||||
micro run
|
||||
@@ -258,16 +254,16 @@ micro run
|
||||
# MCP Tools: http://localhost:8080/mcp/tools
|
||||
```
|
||||
|
||||
You can also scaffold a service from a template:
|
||||
你也可以从模板脚手架生成服务:
|
||||
|
||||
```bash
|
||||
micro new helloworld
|
||||
micro new contacts --template crud
|
||||
```
|
||||
|
||||
## Building Agents
|
||||
## 构建代理
|
||||
|
||||
An Agent is a service with an LLM inside it. It has a proto-defined `Agent.Chat` RPC endpoint, registers in the registry, and is callable like any service:
|
||||
代理是内置 LLM 的服务。它拥有 proto 定义的 `Agent.Chat` RPC 端点,在 registry 中注册,并可像任何服务一样被调用:
|
||||
|
||||
```go
|
||||
agent := micro.NewAgent("task-mgr",
|
||||
@@ -278,7 +274,7 @@ agent := micro.NewAgent("task-mgr",
|
||||
agent.Run()
|
||||
```
|
||||
|
||||
The agent discovers its services from the registry, scopes its tools to their endpoints, and maintains conversation memory in the store. It registers itself so `micro chat` and other agents can find it.
|
||||
代理从 registry 发现其服务,将工具限定到对应端点,并在 store 中维护对话记忆。它会自我注册,以便 `micro chat` 及其他代理能找到它。
|
||||
|
||||
```go
|
||||
// Programmatic interaction
|
||||
@@ -286,21 +282,21 @@ resp, _ := agent.Ask(ctx, "What tasks are overdue?")
|
||||
fmt.Println(resp.Reply)
|
||||
```
|
||||
|
||||
Multiple agents coordinate via RPC — each is a service with an `Agent.Chat` endpoint. `micro chat` routes to the right one.
|
||||
多个代理通过 RPC 协调——每个都是带有 `Agent.Chat` 端点的服务。`micro chat` 会路由到正确的那一个。
|
||||
|
||||
```bash
|
||||
micro agent list # list registered agents
|
||||
micro call task-mgr Agent.Chat '{"message": "What tasks are overdue?"}'
|
||||
```
|
||||
|
||||
### Plan & Delegate
|
||||
### 规划与委托(Plan & Delegate)
|
||||
|
||||
Every agent gets two built-in harness capabilities, exposed as tools — no extra setup or separate graph runtime:
|
||||
每个代理都具备两种内置 harness 能力,以工具形式暴露——无需额外设置或独立的图运行时:
|
||||
|
||||
- **`plan`** — for multi-step work, the agent records an ordered plan in its store-backed memory and stays oriented across turns.
|
||||
- **`delegate`** — the agent hands a self-contained subtask to another agent. If a registered agent already owns the relevant services, the hand-off goes over RPC to that agent; otherwise a focused, short-lived sub-agent is created for the subtask with its own isolated context.
|
||||
- **`plan`** — 对于多步骤工作,代理在其 store 支撑的记忆中记录有序计划,并在多轮对话中保持方向。
|
||||
- **`delegate`** — 代理将自包含的子任务交给另一个代理。若已注册的代理已拥有相关服务,则通过 RPC 交接给该代理;否则会为该子任务创建一个专注、短命的子代理,并配备独立隔离的上下文。
|
||||
|
||||
This keeps intelligence distributed: an agent doesn't need to know *how* to do everything, only *who* does. See [examples/agent-plan-delegate](examples/agent-plan-delegate/).
|
||||
这让智能保持分布式:代理不必知道*如何*完成一切,只需知道*谁*来做。参见 [examples/agent-plan-delegate](examples/agent-plan-delegate/)。
|
||||
|
||||
```go
|
||||
// A sub-agent is just an agent — created with New, talked to with Ask.
|
||||
@@ -308,9 +304,9 @@ This keeps intelligence distributed: an agent doesn't need to know *how* to do e
|
||||
resp, _ := agent.Ask(ctx, "Plan the launch, create the tasks, and have comms notify the owner.")
|
||||
```
|
||||
|
||||
### Batteries included, pluggable
|
||||
### 开箱即用,可插拔
|
||||
|
||||
Just as a service composes pluggable abstractions (registry, broker, store), an agent composes a **model**, **memory**, and **tools** — sane defaults out of the box, each swappable.
|
||||
正如服务组合可插拔抽象(registry、broker、store),代理组合 **model**、**memory** 和 **tools**——开箱即用的合理默认,每一项都可替换。
|
||||
|
||||
```go
|
||||
agent := micro.NewAgent("assistant",
|
||||
@@ -325,11 +321,11 @@ agent := micro.NewAgent("assistant",
|
||||
)
|
||||
```
|
||||
|
||||
**Memory** is durable and store-backed by default (Postgres, NATS KV, or file), so an agent picks up where it left off after a restart — or supply your own with `AgentMemory`. Long-running agents can opt into `AgentCompactMemory(maxMessages, keepRecent)`: older turns are collapsed into a deterministic summary, recent turns stay verbatim, and relevant archived turns are recalled on future asks without replaying the whole conversation. **Tools** are your services automatically, plus any function you register with `AgentTool`.
|
||||
**Memory(记忆)** 默认持久且由 store 支撑(Postgres、NATS KV 或 file),因此代理在重启后可从上次停下的地方继续——也可用 `AgentMemory` 提供你自己的实现。长期运行的代理可选用 `AgentCompactMemory(maxMessages, keepRecent)`:较早轮次被折叠为确定性摘要,最近轮次保持原文,相关归档轮次在未来请求时被召回,而无需重放整段对话。**Tools(工具)** 自动就是你的服务,加上你用 `AgentTool` 注册的任何函数。
|
||||
|
||||
### Paid tools (x402)
|
||||
### 付费工具(x402)
|
||||
|
||||
Every endpoint is an AI-callable tool — and it can be a *paid* tool. Go Micro supports [x402](https://x402.org), the HTTP 402 payment standard for agents, so a tool can require a stablecoin payment and an agent can settle it autonomously. It's opt-in and carries no crypto in the framework: verification is delegated to a pluggable facilitator (Coinbase, Alchemy, self-hosted), so Base and Solana are just different facilitators.
|
||||
每个端点都是 AI 可调用的工具——而且可以是*付费*工具。Go Micro 支持 [x402](https://x402.org), 面向代理的 HTTP 402 支付标准,因此工具可要求稳定币支付,代理可自主结算。这是可选功能,框架本身不携带加密货币逻辑:验证委托给可插拔的 facilitator(Coinbase、Alchemy、自托管),因此 Base 和 Solana 只是不同的 facilitator。
|
||||
|
||||
```bash
|
||||
# Charge for tool calls at the MCP gateway (off unless you set a pay-to address)
|
||||
@@ -338,84 +334,84 @@ micro mcp serve --x402_pay_to 0xYourAddress --x402_network solana --x402_amount
|
||||
micro mcp serve --x402_config x402.json
|
||||
```
|
||||
|
||||
See the [Payments (x402) guide](internal/website/docs/guides/x402-payments.md).
|
||||
参见 [Payments (x402) guide](internal/website/docs/guides/x402-payments.md)。
|
||||
|
||||
### Reachable by other agents (A2A)
|
||||
### 可被其他代理访问(A2A)
|
||||
|
||||
Within a Go Micro system, agents reach each other over RPC. To make them reachable by agents on *other* frameworks, Go Micro speaks the [Agent2Agent (A2A) protocol](https://a2a-protocol.org). The A2A gateway discovers your agents from the registry, generates an Agent Card for each from its metadata — the same way the MCP gateway derives tools from service endpoints — and translates incoming A2A tasks to the agent's `Agent.Chat` RPC. No per-agent code: register an agent and it's reachable over A2A.
|
||||
在 Go Micro 系统内,代理通过 RPC 相互访问。要让*其他*框架上的代理也能访问它们,Go Micro 使用 [Agent2Agent (A2A) protocol](https://a2a-protocol.org). A2A 网关从 registry 发现你的代理,根据其元数据为每个代理生成 Agent Card——与 MCP 网关从服务端点派生工具的方式相同——并将传入的 A2A 任务翻译为代理的 `Agent.Chat` RPC。无需为每个代理编写代码:注册代理即可通过 A2A 访问。
|
||||
|
||||
```bash
|
||||
micro a2a serve --address :4000 # gateway: expose every registered agent over A2A
|
||||
micro a2a list # agents and their Agent Card URLs
|
||||
```
|
||||
|
||||
Or skip the gateway entirely — an agent can serve its own A2A endpoint directly, handling tasks in-process:
|
||||
或者完全跳过网关——代理可直接提供自己的 A2A 端点,在进程内处理任务:
|
||||
|
||||
```go
|
||||
micro.NewAgent("task-mgr", micro.AgentServices("task"), micro.AgentA2A(":4000"))
|
||||
```
|
||||
|
||||
It works both ways. To call an agent on another framework, an `a2a.Client` is wired into the two places that hand off work: `flow.A2A(url)` as a workflow step (the cross-framework `Dispatch`), and `delegate` to an `http(s)` URL from inside an agent.
|
||||
双向皆可。要调用其他框架上的代理,`a2a.Client` 会接入两处交接工作的地方:作为工作流步骤的 `flow.A2A(url)`(跨框架的 `Dispatch`),以及从代理内部向 `http(s)` URL 发起的 `delegate`。
|
||||
|
||||
MCP exposes your services as tools; A2A exposes your agents as agents. See the [A2A guide](internal/website/docs/guides/a2a-protocol.md).
|
||||
MCP 将你的服务暴露为工具;A2A 将你的代理暴露为代理。参见 [A2A guide](internal/website/docs/guides/a2a-protocol.md)。
|
||||
|
||||
## Features
|
||||
## 功能特性
|
||||
|
||||
### AI
|
||||
|
||||
| Feature | Details |
|
||||
| 功能 | 详情 |
|
||||
|---------|---------|
|
||||
| Agents | `micro.NewAgent()` — intelligent layer that manages services |
|
||||
| Plan & delegate | Built-in agent tools — plan multi-step work, delegate subtasks to other agents |
|
||||
| Pluggable memory | Durable store-backed conversation memory by default; swap with `AgentMemory` |
|
||||
| Custom tools | `AgentTool` — give an agent any function as a tool, beyond its services |
|
||||
| Guardrails | `MaxSteps` (stop on count), `LoopLimit` (stop repeated no-progress calls), `ApproveTool` (human-in-the-loop) |
|
||||
| Tool middleware | `AgentWrapTool` — wrap tool execution for logging, metrics, or retries (like client/server wrappers) |
|
||||
| Workflows | `micro.NewFlow()` — event-driven; one step, ordered durable steps, or triggers an agent |
|
||||
| Durable execution | Checkpointed flow steps survive a crash and resume where they stopped; store-backed by default, pluggable backend |
|
||||
| MCP gateway | Every endpoint is an AI tool automatically |
|
||||
| A2A gateway | Every agent is reachable over the Agent2Agent protocol; cards generated from the registry (`micro a2a`) |
|
||||
| Payments (x402) | Opt-in per-call payments for tools via the x402 standard; pluggable facilitator (Base, Solana, …) |
|
||||
| 9 LLM providers | Anthropic, OpenAI, Gemini, Groq, Mistral, Together, Atlas Cloud, MiniMax, Ollama (local + cloud) |
|
||||
| Interactive console | `micro run` includes a chat console for talking to services |
|
||||
| Service generation | `micro run --prompt` — describe a system, get running services |
|
||||
| Agents(智能体) | `micro.NewAgent()` — 管理服务的智能层 |
|
||||
| Plan & delegate(规划与委派) | 内置 agent 工具 — 规划多步骤工作,将子任务委派给其他 agent |
|
||||
| Pluggable memory(可插拔记忆) | 默认基于持久化存储的对话记忆;可通过 `AgentMemory` 替换 |
|
||||
| Custom tools(自定义工具) | `AgentTool` — 为 agent 提供除服务之外的任意函数作为工具 |
|
||||
| Guardrails(护栏) | `MaxSteps`(按次数停止)、`LoopLimit`(停止重复的无进展调用)、`ApproveTool`(human-in-the-loop,人在回路) |
|
||||
| Tool middleware(工具中间件) | `AgentWrapTool` — 包装工具执行以实现日志、指标或重试(类似 client/server wrappers) |
|
||||
| Workflows(工作流) | `micro.NewFlow()` — 事件驱动;单步、有序持久化步骤,或触发 agent |
|
||||
| Durable execution(持久化执行) | 带检查点的流程步骤可在崩溃后存活并从停止处恢复;默认基于存储,后端可插拔 |
|
||||
| MCP gateway | 每个 endpoint 自动成为 AI 工具 |
|
||||
| A2A gateway | 每个 agent 均可通过 Agent2Agent 协议访问;卡片由 registry(`micro a2a`)生成 |
|
||||
| Payments (x402) | 通过 x402 标准按调用选择性为工具付费;facilitator 可插拔(Base、Solana 等) |
|
||||
| 9 个 LLM 提供商 | Anthropic、OpenAI、Gemini、Groq、Mistral、Together、Atlas Cloud、MiniMax、Ollama(本地 + 云端) |
|
||||
| Interactive console(交互式控制台) | `micro run` 包含与服务对话的聊天控制台 |
|
||||
| Service generation(服务生成) | `micro run --prompt` — 描述系统即可获得运行中的服务 |
|
||||
|
||||
### Framework
|
||||
### 框架
|
||||
|
||||
| Feature | Details |
|
||||
| 功能 | 详情 |
|
||||
|---------|---------|
|
||||
| Service registry | mDNS (default), Consul, etcd |
|
||||
| RPC client/server | gRPC transport, load balancing, streaming |
|
||||
| Pub/sub events | NATS, RabbitMQ, HTTP broker |
|
||||
| Key-value store | File (bbolt), Postgres, NATS KV |
|
||||
| Typed model layer | CRUD + queries, SQLite/Postgres backends |
|
||||
| Everything swappable | All abstractions are Go interfaces |
|
||||
| Service registry(服务注册) | mDNS(默认)、Consul、etcd |
|
||||
| RPC client/server | gRPC 传输、负载均衡、流式传输 |
|
||||
| Pub/sub events(发布/订阅事件) | NATS、RabbitMQ、HTTP broker |
|
||||
| Key-value store(键值存储) | File (bbolt)、Postgres、NATS KV |
|
||||
| Typed model layer(类型化模型层) | CRUD + 查询,SQLite/Postgres 后端 |
|
||||
| Everything swappable(一切皆可替换) | 所有抽象均为 Go interface |
|
||||
|
||||
### Developer experience & deployment
|
||||
### 开发者体验与部署
|
||||
|
||||
| Feature | Details |
|
||||
| 功能 | 详情 |
|
||||
|---------|---------|
|
||||
| Hot reload | `micro run` watches files, rebuilds on change |
|
||||
| Templates | `micro new --template crud/pubsub/api` |
|
||||
| One-command deploy | `micro deploy user@server` — SSH + systemd, no Docker |
|
||||
| Hot reload(热重载) | `micro run` 监听文件变更并重新构建 |
|
||||
| Templates(模板) | `micro new --template crud/pubsub/api` |
|
||||
| One-command deploy(一键部署) | `micro deploy user@server` — SSH + systemd,无需 Docker |
|
||||
|
||||
## CLI
|
||||
|
||||
| Command | Purpose |
|
||||
| 命令 | 用途 |
|
||||
|---------|---------|
|
||||
| `micro run --prompt "..."` | Generate services + agent, start with interactive console |
|
||||
| `micro run` | Dev mode: hot reload, gateway, interactive console |
|
||||
| `micro run -d` | Detached mode (no console) |
|
||||
| `micro chat` | Standalone chat (when not using micro run) |
|
||||
| `micro agent list` | List registered agents |
|
||||
| `micro new myservice` | Scaffold a service |
|
||||
| `micro call service endpoint '{}'` | Call a service or agent from the CLI |
|
||||
| `micro build` | Compile production binaries |
|
||||
| `micro deploy user@server` | Deploy via SSH + systemd |
|
||||
| `micro run --prompt "..."` | 生成服务与 agent,并启动交互式控制台 |
|
||||
| `micro run` | 开发模式:热重载、gateway、交互式控制台 |
|
||||
| `micro run -d` | 分离模式(无控制台) |
|
||||
| `micro chat` | 独立聊天(未使用 micro run 时) |
|
||||
| `micro agent list` | 列出已注册的 agent |
|
||||
| `micro new myservice` | 脚手架生成服务 |
|
||||
| `micro call service endpoint '{}'` | 从 CLI 调用服务或 agent |
|
||||
| `micro build` | 编译生产二进制文件 |
|
||||
| `micro deploy user@server` | 通过 SSH + systemd 部署 |
|
||||
|
||||
## Multi-Service Projects
|
||||
## 多服务项目
|
||||
|
||||
Run multiple services together:
|
||||
同时运行多个服务:
|
||||
|
||||
```go
|
||||
users := micro.NewService("users", micro.Address(":9001"))
|
||||
@@ -428,7 +424,7 @@ g := micro.NewGroup(users, orders)
|
||||
g.Run()
|
||||
```
|
||||
|
||||
Or use a `micro.mu` config file:
|
||||
或使用 `micro.mu` 配置文件:
|
||||
|
||||
```
|
||||
service users
|
||||
@@ -439,9 +435,9 @@ service orders
|
||||
depends users
|
||||
```
|
||||
|
||||
## Data Model
|
||||
## 数据模型
|
||||
|
||||
Typed persistence with CRUD and queries:
|
||||
类型化持久化,支持 CRUD 与查询:
|
||||
|
||||
```go
|
||||
type User struct {
|
||||
@@ -458,13 +454,13 @@ var results []*User
|
||||
db.List(ctx, &results, model.Where("email", "alice@example.com"))
|
||||
```
|
||||
|
||||
Backends: memory (default), SQLite, Postgres.
|
||||
后端:memory(默认)、SQLite、Postgres。
|
||||
|
||||
## AI Providers
|
||||
## AI 提供商
|
||||
|
||||
Swap providers with a single import — same interface everywhere:
|
||||
单次 import 即可切换提供商 — 各处使用相同 interface:
|
||||
|
||||
| Provider | Default Model |
|
||||
| 提供商 | 默认模型 |
|
||||
|----------|---------------|
|
||||
| Anthropic | `claude-sonnet-4-20250514` |
|
||||
| OpenAI | `gpt-4o` |
|
||||
@@ -481,34 +477,34 @@ m := ai.New("anthropic", ai.WithAPIKey(key))
|
||||
resp, _ := m.Generate(ctx, &ai.Request{Prompt: "hello"})
|
||||
```
|
||||
|
||||
## Examples
|
||||
## 示例
|
||||
|
||||
New to agents? Follow the [first-agent on-ramp](#first-agent-on-ramp), then use the [examples index](examples/README.md) for the full services → agents → workflows map.
|
||||
初次接触 agent?请跟随 [first-agent on-ramp](#first-agent-on-ramp),然后使用 [examples index](examples/README.md) 查看完整的 services → agents → workflows 映射。
|
||||
|
||||
- [hello-world](examples/hello-world/) — Basic RPC service
|
||||
- [multi-service](examples/multi-service/) — Multiple services in one binary
|
||||
- [mcp](examples/mcp/) — MCP integration with AI agents
|
||||
- [first-agent](examples/first-agent/) — Smallest provider-free service-backed agent
|
||||
- [agent-plan-delegate](examples/agent-plan-delegate/) — Agent planning and multi-agent delegation
|
||||
- [agent-durable](examples/agent-durable/) — Checkpoint and resume an agent run without replaying completed tool side effects
|
||||
- [grpc-interop](examples/grpc-interop/) — Call go-micro from any gRPC client
|
||||
- [hello-world](examples/hello-world/) — 基础 RPC 服务
|
||||
- [multi-service](examples/multi-service/) — 单个二进制文件中运行多个服务
|
||||
- [mcp](examples/mcp/) — 与 AI agent 的 MCP 集成
|
||||
- [first-agent](examples/first-agent/) — 最小化、无需 provider 的服务支撑型 agent
|
||||
- [agent-plan-delegate](examples/agent-plan-delegate/) — Agent 规划与多 agent 委派
|
||||
- [agent-durable](examples/agent-durable/) — 检查点与恢复 agent 运行,无需重放已完成的工具副作用
|
||||
- [grpc-interop](examples/grpc-interop/) — 从任意 gRPC 客户端调用 go-micro
|
||||
|
||||
See [all examples](examples/README.md).
|
||||
查看 [all examples](examples/README.md)。
|
||||
|
||||
## Docs
|
||||
## 文档
|
||||
|
||||
- [Getting Started](internal/website/docs/getting-started.md)
|
||||
- [AI Integration](internal/website/docs/ai-integration.md)
|
||||
- [Your First Agent](internal/website/docs/guides/your-first-agent.md)
|
||||
- [0→hero Reference](internal/website/docs/guides/zero-to-hero.md)
|
||||
- [Agents and Workflows](internal/website/docs/guides/agents-and-workflows.md)
|
||||
- [Agent Design](internal/docs/AGENT_DESIGN.md)
|
||||
- [Plan & Delegate](internal/website/docs/guides/plan-delegate.md)
|
||||
- [Agent Guardrails](internal/website/docs/guides/agent-guardrails.md)
|
||||
- [Payments (x402)](internal/website/docs/guides/x402-payments.md)
|
||||
- [MCP & AI Agents](internal/website/docs/mcp.md)
|
||||
- [Data Model](internal/website/docs/model.md)
|
||||
- [Deployment](internal/website/docs/deployment.md)
|
||||
- [Plugins](internal/website/docs/plugins.md)
|
||||
- [入门指南](internal/website/docs/getting-started.md)
|
||||
- [AI 集成](internal/website/docs/ai-integration.md)
|
||||
- [你的第一个 Agent](internal/website/docs/guides/your-first-agent.md)
|
||||
- [0→hero 参考](internal/website/docs/guides/zero-to-hero.md)
|
||||
- [Agent 与工作流](internal/website/docs/guides/agents-and-workflows.md)
|
||||
- [Agent 设计](internal/docs/AGENT_DESIGN.md)
|
||||
- [规划与委派](internal/website/docs/guides/plan-delegate.md)
|
||||
- [Agent 护栏](internal/website/docs/guides/agent-guardrails.md)
|
||||
- [支付(x402)](internal/website/docs/guides/x402-payments.md)
|
||||
- [MCP 与 AI Agent](internal/website/docs/mcp.md)
|
||||
- [数据模型](internal/website/docs/model.md)
|
||||
- [部署](internal/website/docs/deployment.md)
|
||||
- [插件](internal/website/docs/plugins.md)
|
||||
|
||||
Package reference: https://pkg.go.dev/go-micro.dev/v6
|
||||
包参考:https://pkg.go.dev/go-micro.dev/v6
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
# WeHub 来源说明
|
||||
|
||||
- 原始项目:`micro/go-micro`
|
||||
- 原始仓库:https://github.com/micro/go-micro
|
||||
- 导入方式:上游默认分支的最新快照
|
||||
- 原作者、版权和许可证信息以原始仓库及本仓库 LICENSE 为准
|
||||
- 本文件仅用于记录来源,不代表 WeHub 是原项目作者
|
||||
+65
-21
@@ -68,35 +68,79 @@ func TestA2AStreamUsesAgentChatPathWithTools(t *testing.T) {
|
||||
t.Fatalf("stream body missing tool marker: %s", rr.Body.String())
|
||||
}
|
||||
|
||||
var final struct {
|
||||
Result struct {
|
||||
Status struct {
|
||||
State string `json:"state"`
|
||||
} `json:"status"`
|
||||
Artifacts []struct {
|
||||
Parts []struct {
|
||||
Text string `json:"text"`
|
||||
} `json:"parts"`
|
||||
} `json:"artifacts"`
|
||||
} `json:"result"`
|
||||
Error any `json:"error"`
|
||||
}
|
||||
// The spec-shaped stream carries the answer as append artifact-update
|
||||
// deltas and closes with a completed status-update (final:true).
|
||||
var (
|
||||
text strings.Builder
|
||||
finalState string
|
||||
sawFinal bool
|
||||
)
|
||||
for _, line := range strings.Split(strings.TrimSpace(rr.Body.String()), "\n") {
|
||||
line = strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(line), "data: "))
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
if err := json.Unmarshal([]byte(line), &final); err != nil {
|
||||
var ev struct {
|
||||
Result json.RawMessage `json:"result"`
|
||||
Error any `json:"error"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(line), &ev); err != nil {
|
||||
t.Fatalf("decode event %q: %v", line, err)
|
||||
}
|
||||
if ev.Error != nil {
|
||||
t.Fatalf("event carried an error field: %+v", ev.Error)
|
||||
}
|
||||
var kind struct {
|
||||
Kind string `json:"kind"`
|
||||
}
|
||||
_ = json.Unmarshal(ev.Result, &kind)
|
||||
switch kind.Kind {
|
||||
case "artifact-update":
|
||||
var au struct {
|
||||
Artifact struct {
|
||||
Parts []struct {
|
||||
Text string `json:"text"`
|
||||
} `json:"parts"`
|
||||
} `json:"artifact"`
|
||||
}
|
||||
_ = json.Unmarshal(ev.Result, &au)
|
||||
for _, p := range au.Artifact.Parts {
|
||||
text.WriteString(p.Text)
|
||||
}
|
||||
case "status-update":
|
||||
var su struct {
|
||||
Status struct {
|
||||
State string `json:"state"`
|
||||
} `json:"status"`
|
||||
Final bool `json:"final"`
|
||||
}
|
||||
_ = json.Unmarshal(ev.Result, &su)
|
||||
if su.Final {
|
||||
sawFinal = true
|
||||
finalState = su.Status.State
|
||||
}
|
||||
default: // opening "task" snapshot
|
||||
var task struct {
|
||||
Artifacts []struct {
|
||||
Parts []struct {
|
||||
Text string `json:"text"`
|
||||
} `json:"parts"`
|
||||
} `json:"artifacts"`
|
||||
}
|
||||
_ = json.Unmarshal(ev.Result, &task)
|
||||
for _, a := range task.Artifacts {
|
||||
for _, p := range a.Parts {
|
||||
if p.Text != "" {
|
||||
text.WriteString(p.Text)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if final.Error != nil {
|
||||
t.Fatalf("final event error: %+v", final.Error)
|
||||
if !sawFinal || finalState != "completed" {
|
||||
t.Fatalf("want a completed final:true status-update; sawFinal=%v state=%q", sawFinal, finalState)
|
||||
}
|
||||
if final.Result.Status.State != "completed" {
|
||||
t.Fatalf("final state = %q, want completed", final.Result.Status.State)
|
||||
}
|
||||
if len(final.Result.Artifacts) != 1 || len(final.Result.Artifacts[0].Parts) != 1 || !strings.Contains(final.Result.Artifacts[0].Parts[0].Text, "a2a-stream-ok") {
|
||||
t.Fatalf("final artifacts = %+v, want tool marker", final.Result.Artifacts)
|
||||
if !strings.Contains(text.String(), "a2a-stream-ok") {
|
||||
t.Fatalf("reassembled stream text missing tool marker: %q", text.String())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -451,6 +451,11 @@ func (a *agentImpl) spendWrap(next ai.ToolHandler) ai.ToolHandler {
|
||||
amount, call.Name, a.spend, a.opts.MaxSpend))
|
||||
}
|
||||
a.spend += amount
|
||||
if info, ok := ai.RunInfoFrom(ctx); ok {
|
||||
info.Spent = a.spend
|
||||
info.ToolSpend = amount
|
||||
ctx = ai.WithRunInfo(ctx, info)
|
||||
}
|
||||
res := next(ctx, call)
|
||||
if res.Refused != "" || toolErrorMessage(res) != "" {
|
||||
a.spend -= amount
|
||||
|
||||
@@ -211,7 +211,8 @@ func TestAgentPayerPaysX402ToolResultAndRetries(t *testing.T) {
|
||||
defer srv.Close()
|
||||
|
||||
payer := &agentMockPayer{}
|
||||
a := newTestAgent(Name("x402-payer"), Payer(payer), Budget(10), WithTool("paid.http", "paid http", nil, func(ctx context.Context, input map[string]any) (string, error) {
|
||||
st := store.NewMemoryStore()
|
||||
a := newTestAgent(Name("x402-payer"), WithStore(st), Payer(payer), Budget(10), WithTool("paid.http", "paid http", nil, func(ctx context.Context, input map[string]any) (string, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -228,13 +229,21 @@ func TestAgentPayerPaysX402ToolResultAndRetries(t *testing.T) {
|
||||
return string(body), nil
|
||||
}))
|
||||
|
||||
res := a.toolHandler()(context.Background(), ai.ToolCall{ID: "pay-1", Name: "paid.http", Input: map[string]any{"url": srv.URL}})
|
||||
ctx := ai.WithRunInfo(context.Background(), ai.RunInfo{RunID: "run-paid", Agent: "x402-payer"})
|
||||
res := a.toolHandler()(ctx, ai.ToolCall{ID: "pay-1", Name: "paid.http", Input: map[string]any{"url": srv.URL}})
|
||||
if !paid || payer.calls != 1 {
|
||||
t.Fatalf("payment not made: paid=%v payer.calls=%d", paid, payer.calls)
|
||||
}
|
||||
if res.Content != `{"ok":true}` || res.Attempts != 2 {
|
||||
t.Fatalf("result = %+v, want paid response with retry attempt", res)
|
||||
}
|
||||
events, err := LoadRunEvents(st, "x402-payer", "run-paid")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(events) != 1 || events[0].Spent != 7 || events[0].ToolSpend != 7 {
|
||||
t.Fatalf("spend events = %#v, want one tool event with spent/tool_spend 7", events)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgentPayerRefusesX402OverBudget(t *testing.T) {
|
||||
|
||||
+27
-2
@@ -51,6 +51,8 @@ const (
|
||||
AttrDispatch = "agent.dispatch"
|
||||
AttrTrigger = "agent.trigger"
|
||||
AttrRunEventKind = "agent.event.kind"
|
||||
AttrSpend = "agent.spend"
|
||||
AttrToolSpend = "agent.tool.spend"
|
||||
)
|
||||
|
||||
type RunEvent struct {
|
||||
@@ -73,6 +75,8 @@ type RunEvent struct {
|
||||
Error string `json:"error,omitempty"`
|
||||
ErrorKind string `json:"error_kind,omitempty"`
|
||||
InputChars int `json:"input_chars,omitempty"`
|
||||
Spent int64 `json:"spent,omitempty"`
|
||||
ToolSpend int64 `json:"tool_spend,omitempty"`
|
||||
}
|
||||
|
||||
type Usage = ai.Usage
|
||||
@@ -111,6 +115,7 @@ type RunSummary struct {
|
||||
LastKind string `json:"last_kind,omitempty"`
|
||||
LastError string `json:"last_error,omitempty"`
|
||||
LastErrorKind string `json:"last_error_kind,omitempty"`
|
||||
Spent int64 `json:"spent,omitempty"`
|
||||
}
|
||||
|
||||
func (a *agentImpl) tracer() trace.Tracer {
|
||||
@@ -362,6 +367,7 @@ func (a *agentImpl) traceTool(next ai.ToolHandler) ai.ToolHandler {
|
||||
return func(ctx context.Context, call ai.ToolCall) ai.ToolResult {
|
||||
info, _ := ai.RunInfoFrom(ctx)
|
||||
start := time.Now()
|
||||
spentBefore := a.spend
|
||||
|
||||
if a.opts.TraceProvider == nil {
|
||||
res := next(ctx, call)
|
||||
@@ -371,7 +377,7 @@ func (a *agentImpl) traceTool(next ai.ToolHandler) ai.ToolHandler {
|
||||
if toolAttempts <= 0 {
|
||||
toolAttempts = 1
|
||||
}
|
||||
a.recordRunEvent(RunEvent{Time: time.Now(), RunID: info.RunID, ParentID: info.ParentID, Agent: info.Agent, Kind: "tool", Name: call.Name, Attempt: toolAttempts, MaxAttempts: a.opts.ToolMaxAttempts, LatencyMS: dur, Refused: res.Refused, Error: resErr, ErrorKind: classifyToolError(resErr)})
|
||||
a.recordRunEvent(RunEvent{Time: time.Now(), RunID: info.RunID, ParentID: info.ParentID, Agent: info.Agent, Kind: "tool", Name: call.Name, Attempt: toolAttempts, MaxAttempts: a.opts.ToolMaxAttempts, LatencyMS: dur, Refused: res.Refused, Error: resErr, ErrorKind: classifyToolError(resErr), Spent: a.spend, ToolSpend: a.spend - spentBefore})
|
||||
return res
|
||||
}
|
||||
|
||||
@@ -385,6 +391,7 @@ func (a *agentImpl) traceTool(next ai.ToolHandler) ai.ToolHandler {
|
||||
ctx, span := a.tracer().Start(ctx, spanNameToolCall, trace.WithAttributes(spanAttrs...))
|
||||
res := next(ctx, call)
|
||||
dur := time.Since(start).Milliseconds()
|
||||
toolSpend := a.spend - spentBefore
|
||||
attrs := []attribute.KeyValue{attribute.Int64(AttrLatencyMS, dur)}
|
||||
toolAttempts := res.Attempts
|
||||
if toolAttempts <= 0 {
|
||||
@@ -394,6 +401,9 @@ func (a *agentImpl) traceTool(next ai.ToolHandler) ai.ToolHandler {
|
||||
if a.opts.ToolMaxAttempts > 0 {
|
||||
attrs = append(attrs, attribute.Int(AttrToolMaxAttempts, a.opts.ToolMaxAttempts))
|
||||
}
|
||||
if toolSpend > 0 {
|
||||
attrs = append(attrs, attribute.Int64(AttrSpend, a.spend), attribute.Int64(AttrToolSpend, toolSpend))
|
||||
}
|
||||
if res.Refused != "" {
|
||||
attrs = append(attrs, attribute.Bool(AttrGuardrailBlock, true), attribute.String(AttrRefusal, res.Refused))
|
||||
}
|
||||
@@ -409,7 +419,7 @@ func (a *agentImpl) traceTool(next ai.ToolHandler) ai.ToolHandler {
|
||||
} else {
|
||||
span.SetStatus(codes.Ok, "")
|
||||
}
|
||||
a.recordSpanEvent(span, RunEvent{Time: time.Now(), RunID: info.RunID, ParentID: info.ParentID, Agent: info.Agent, Kind: "tool", Name: call.Name, Attempt: toolAttempts, MaxAttempts: a.opts.ToolMaxAttempts, LatencyMS: dur, Refused: res.Refused, Error: resErr, ErrorKind: classifyToolError(resErr)})
|
||||
a.recordSpanEvent(span, RunEvent{Time: time.Now(), RunID: info.RunID, ParentID: info.ParentID, Agent: info.Agent, Kind: "tool", Name: call.Name, Attempt: toolAttempts, MaxAttempts: a.opts.ToolMaxAttempts, LatencyMS: dur, Refused: res.Refused, Error: resErr, ErrorKind: classifyToolError(resErr), Spent: a.spend, ToolSpend: toolSpend})
|
||||
span.End()
|
||||
return res
|
||||
}
|
||||
@@ -496,6 +506,12 @@ func runEventAttributes(e RunEvent) []attribute.KeyValue {
|
||||
if e.InputChars > 0 {
|
||||
attrs = append(attrs, attribute.Int(AttrInputChars, e.InputChars))
|
||||
}
|
||||
if e.Spent > 0 {
|
||||
attrs = append(attrs, attribute.Int64(AttrSpend, e.Spent))
|
||||
}
|
||||
if e.ToolSpend > 0 {
|
||||
attrs = append(attrs, attribute.Int64(AttrToolSpend, e.ToolSpend))
|
||||
}
|
||||
attrs = appendUsage(attrs, e.Tokens)
|
||||
if e.Refused != "" {
|
||||
attrs = append(attrs, attribute.Bool(AttrGuardrailBlock, true), attribute.String(AttrRefusal, e.Refused))
|
||||
@@ -530,6 +546,12 @@ func appendRunInfoAttributes(attrs []attribute.KeyValue, info ai.RunInfo) []attr
|
||||
if info.Trigger != "" {
|
||||
attrs = append(attrs, attribute.String(AttrTrigger, info.Trigger))
|
||||
}
|
||||
if info.Spent > 0 {
|
||||
attrs = append(attrs, attribute.Int64(AttrSpend, info.Spent))
|
||||
}
|
||||
if info.ToolSpend > 0 {
|
||||
attrs = append(attrs, attribute.Int64(AttrToolSpend, info.ToolSpend))
|
||||
}
|
||||
return attrs
|
||||
}
|
||||
|
||||
@@ -616,6 +638,9 @@ func ListRunSummariesWithOptions(s store.Store, agentName string, opts RunListOp
|
||||
if e.ErrorKind != "" {
|
||||
summary.LastErrorKind = e.ErrorKind
|
||||
}
|
||||
if e.Spent > summary.Spent {
|
||||
summary.Spent = e.Spent
|
||||
}
|
||||
}
|
||||
if opts.Status != "" && summary.Status != opts.Status {
|
||||
continue
|
||||
|
||||
@@ -421,6 +421,63 @@ func spanAttributes(attrs []attribute.KeyValue) map[string]string {
|
||||
return out
|
||||
}
|
||||
|
||||
func TestAgentOpenTelemetryToolSpanIncludesSpend(t *testing.T) {
|
||||
exp := tracetest.NewInMemoryExporter()
|
||||
tp := trace.NewTracerProvider(trace.WithSyncer(exp))
|
||||
st := store.NewMemoryStore()
|
||||
a := New(Name("spender"), Provider("oteltest"), Model("unit-model"), WithStore(st), TraceProvider(tp), MaxSpend(10), ToolSpend("probe", 7), WithTool("probe", "probe", nil, func(ctx context.Context, input map[string]any) (string, error) {
|
||||
info, ok := ai.RunInfoFrom(ctx)
|
||||
if !ok {
|
||||
t.Fatal("RunInfo missing from paid tool context")
|
||||
}
|
||||
if info.Spent != 7 || info.ToolSpend != 7 {
|
||||
t.Fatalf("RunInfo spend = (%d, %d), want (7, 7)", info.Spent, info.ToolSpend)
|
||||
}
|
||||
return "ok", nil
|
||||
}))
|
||||
if _, err := a.Ask(context.Background(), "hello"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var sawToolSpan bool
|
||||
for _, s := range exp.GetSpans().Snapshots() {
|
||||
if s.Name() != spanNameToolCall {
|
||||
continue
|
||||
}
|
||||
sawToolSpan = true
|
||||
attrs := spanAttributes(s.Attributes())
|
||||
if attrs[AttrSpend] != "7" || attrs[AttrToolSpend] != "7" {
|
||||
t.Fatalf("tool span missing spend attributes: %#v", attrs)
|
||||
}
|
||||
if !spanEventHasAttribute(s.Events(), "agent.tool", AttrToolSpend, "7") {
|
||||
t.Fatalf("tool event missing spend attribute: %#v", s.Events())
|
||||
}
|
||||
}
|
||||
if !sawToolSpan {
|
||||
t.Fatal("tool span not emitted")
|
||||
}
|
||||
summaries, err := ListRunSummaries(st, "spender")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(summaries) != 1 || summaries[0].Spent != 7 {
|
||||
t.Fatalf("summary spend = %#v, want 7", summaries)
|
||||
}
|
||||
}
|
||||
|
||||
func spanEventHasAttribute(events []trace.Event, name, key, value string) bool {
|
||||
for _, e := range events {
|
||||
if e.Name != name {
|
||||
continue
|
||||
}
|
||||
attrs := spanAttributes(e.Attributes)
|
||||
if attrs[key] == value {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func TestAgentOpenTelemetrySpansDelegateLineage(t *testing.T) {
|
||||
exp := tracetest.NewInMemoryExporter()
|
||||
tp := trace.NewTracerProvider(trace.WithSyncer(exp))
|
||||
|
||||
@@ -135,6 +135,8 @@ type RunInfo struct {
|
||||
VerificationFeedback string // feedback from the previous failed verifier attempt, when retrying a flow step
|
||||
Dispatch string // how the run was dispatched (direct, broker, schedule, resume) when known
|
||||
Trigger string // external trigger or schedule label that started the run, when known
|
||||
Spent int64 // cumulative paid x402 spend in this run, in the asset's smallest unit
|
||||
ToolSpend int64 // paid x402 spend attributed to the current tool call, in the asset's smallest unit
|
||||
}
|
||||
|
||||
type runInfoKey struct{}
|
||||
|
||||
@@ -94,6 +94,9 @@ func writeAgentInspection(w io.Writer, name string, runs []goagent.RunSummary, a
|
||||
if run.LastErrorKind != "" {
|
||||
fmt.Fprintf(w, " error_kind=%s", run.LastErrorKind)
|
||||
}
|
||||
if run.Spent > 0 {
|
||||
fmt.Fprintf(w, " spent=%d", run.Spent)
|
||||
}
|
||||
if run.LastError != "" {
|
||||
fmt.Fprintf(w, " error=%q", run.LastError)
|
||||
}
|
||||
|
||||
@@ -11,13 +11,13 @@ import (
|
||||
)
|
||||
|
||||
func TestWriteAgentInspectionIncludesActionableBreadcrumbs(t *testing.T) {
|
||||
runs := []goagent.RunSummary{{RunID: "run-1", Status: "auth", Events: 4, LastKind: "model", LastError: "invalid API key", LastErrorKind: "auth", TraceID: "1234567890abcdef", Checkpoint: "failed", Stage: "ask"}}
|
||||
runs := []goagent.RunSummary{{RunID: "run-1", Status: "auth", Events: 4, LastKind: "model", LastError: "invalid API key", LastErrorKind: "auth", TraceID: "1234567890abcdef", Checkpoint: "failed", Stage: "ask", Spent: 7}}
|
||||
var out bytes.Buffer
|
||||
if err := writeAgentInspection(&out, "support", runs, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := out.String()
|
||||
for _, want := range []string{"Agent \"support\" runs", "run-1", "status=auth", "events=4", "last=model", "checkpoint=failed", "stage=ask", "error_kind=auth", `error="invalid API key"`, "trace=1234567890ab"} {
|
||||
for _, want := range []string{"Agent \"support\" runs", "run-1", "status=auth", "events=4", "last=model", "checkpoint=failed", "stage=ask", "error_kind=auth", `error="invalid API key"`, "trace=1234567890ab", "spent=7"} {
|
||||
if !strings.Contains(got, want) {
|
||||
t.Fatalf("output missing %q:\n%s", want, got)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
# Kubernetes deployment foundation (alpha)
|
||||
|
||||
This package is the first opt-in Kubernetes foundation for the Go Micro lifecycle:
|
||||
`Service`, `Agent`, and `Flow` resources. It is intentionally experimental and
|
||||
additive. Nothing in the Go Micro runtime installs these resources or changes
|
||||
production defaults.
|
||||
|
||||
## What is included
|
||||
|
||||
- Alpha CRD manifests in `config/crd/` for `agents.micro.dev`,
|
||||
`services.micro.dev`, and `flows.micro.dev`.
|
||||
- A small dependency-free mapper that turns a desired Go Micro resource into the
|
||||
Kubernetes `Deployment` shape an operator reconciliation loop will own.
|
||||
- Unit tests that validate the structural CRD fragments and dry-run the
|
||||
Agent-to-Deployment mapping.
|
||||
|
||||
## Local validation
|
||||
|
||||
```sh
|
||||
go test ./deploy/kubernetes
|
||||
```
|
||||
|
||||
If you have a Kubernetes cluster and `kubectl` available, you can also perform a
|
||||
server-side dry run of the CRDs:
|
||||
|
||||
```sh
|
||||
kubectl apply --dry-run=server -f deploy/kubernetes/config/crd/
|
||||
```
|
||||
|
||||
The manifests are `v1alpha1`; expect the API shape to evolve before this becomes
|
||||
a production operator.
|
||||
@@ -0,0 +1,37 @@
|
||||
apiVersion: apiextensions.k8s.io/v1
|
||||
kind: CustomResourceDefinition
|
||||
metadata:
|
||||
name: agents.micro.dev
|
||||
spec:
|
||||
group: micro.dev
|
||||
scope: Namespaced
|
||||
names:
|
||||
plural: agents
|
||||
singular: agent
|
||||
kind: Agent
|
||||
shortNames: [magent]
|
||||
versions:
|
||||
- name: v1alpha1
|
||||
served: true
|
||||
storage: true
|
||||
schema:
|
||||
openAPIV3Schema:
|
||||
type: object
|
||||
required: [spec]
|
||||
properties:
|
||||
spec:
|
||||
type: object
|
||||
required: [image]
|
||||
properties:
|
||||
image: {type: string, minLength: 1}
|
||||
command:
|
||||
type: array
|
||||
items: {type: string}
|
||||
args:
|
||||
type: array
|
||||
items: {type: string}
|
||||
replicas: {type: integer, minimum: 0}
|
||||
registry: {type: string}
|
||||
env:
|
||||
type: object
|
||||
additionalProperties: {type: string}
|
||||
@@ -0,0 +1,37 @@
|
||||
apiVersion: apiextensions.k8s.io/v1
|
||||
kind: CustomResourceDefinition
|
||||
metadata:
|
||||
name: flows.micro.dev
|
||||
spec:
|
||||
group: micro.dev
|
||||
scope: Namespaced
|
||||
names:
|
||||
plural: flows
|
||||
singular: flow
|
||||
kind: Flow
|
||||
shortNames: [mflow]
|
||||
versions:
|
||||
- name: v1alpha1
|
||||
served: true
|
||||
storage: true
|
||||
schema:
|
||||
openAPIV3Schema:
|
||||
type: object
|
||||
required: [spec]
|
||||
properties:
|
||||
spec:
|
||||
type: object
|
||||
required: [image]
|
||||
properties:
|
||||
image: {type: string, minLength: 1}
|
||||
command:
|
||||
type: array
|
||||
items: {type: string}
|
||||
args:
|
||||
type: array
|
||||
items: {type: string}
|
||||
replicas: {type: integer, minimum: 0}
|
||||
registry: {type: string}
|
||||
env:
|
||||
type: object
|
||||
additionalProperties: {type: string}
|
||||
@@ -0,0 +1,37 @@
|
||||
apiVersion: apiextensions.k8s.io/v1
|
||||
kind: CustomResourceDefinition
|
||||
metadata:
|
||||
name: services.micro.dev
|
||||
spec:
|
||||
group: micro.dev
|
||||
scope: Namespaced
|
||||
names:
|
||||
plural: services
|
||||
singular: service
|
||||
kind: Service
|
||||
shortNames: [mservice]
|
||||
versions:
|
||||
- name: v1alpha1
|
||||
served: true
|
||||
storage: true
|
||||
schema:
|
||||
openAPIV3Schema:
|
||||
type: object
|
||||
required: [spec]
|
||||
properties:
|
||||
spec:
|
||||
type: object
|
||||
required: [image]
|
||||
properties:
|
||||
image: {type: string, minLength: 1}
|
||||
command:
|
||||
type: array
|
||||
items: {type: string}
|
||||
args:
|
||||
type: array
|
||||
items: {type: string}
|
||||
replicas: {type: integer, minimum: 0}
|
||||
registry: {type: string}
|
||||
env:
|
||||
type: object
|
||||
additionalProperties: {type: string}
|
||||
@@ -0,0 +1,8 @@
|
||||
// Package kubernetes contains the experimental Kubernetes deployment foundation
|
||||
// for Go Micro services, agents, and flows.
|
||||
//
|
||||
// The package is intentionally small and additive: it exposes alpha custom
|
||||
// resource manifests and a dry-run mapper that turns a resource spec into the
|
||||
// Deployment shape an operator would reconcile. It does not install an operator
|
||||
// or change any runtime defaults.
|
||||
package kubernetes
|
||||
@@ -0,0 +1,87 @@
|
||||
package kubernetes
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCRDManifestsAreStructural(t *testing.T) {
|
||||
for _, kind := range []Kind{KindAgent, KindService, KindFlow} {
|
||||
manifest := CRDManifests[kind]
|
||||
if manifest == "" {
|
||||
t.Fatalf("missing manifest for %s", kind)
|
||||
}
|
||||
checks := []string{
|
||||
"apiVersion: apiextensions.k8s.io/v1",
|
||||
"kind: CustomResourceDefinition",
|
||||
"group: micro.dev",
|
||||
"kind: " + string(kind),
|
||||
"name: v1alpha1",
|
||||
"served: true",
|
||||
"storage: true",
|
||||
"openAPIV3Schema:",
|
||||
"type: object",
|
||||
"required: [image]",
|
||||
}
|
||||
for _, check := range checks {
|
||||
if !strings.Contains(manifest, check) {
|
||||
t.Fatalf("%s manifest missing %q:\n%s", kind, check, manifest)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapDeploymentForAgent(t *testing.T) {
|
||||
deployment, err := MapDeployment(Resource{
|
||||
Kind: KindAgent,
|
||||
Name: "support-agent",
|
||||
Namespace: "agents",
|
||||
Spec: WorkloadSpec{
|
||||
Image: "ghcr.io/acme/support-agent:v1",
|
||||
Replicas: 2,
|
||||
Registry: "kubernetes",
|
||||
Environment: map[string]string{
|
||||
"MODEL": "gpt-5.5",
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("MapDeployment returned error: %v", err)
|
||||
}
|
||||
if deployment.Name != "support-agent" || deployment.Namespace != "agents" {
|
||||
t.Fatalf("unexpected identity: %+v", deployment)
|
||||
}
|
||||
if deployment.Replicas != 2 {
|
||||
t.Fatalf("replicas = %d, want 2", deployment.Replicas)
|
||||
}
|
||||
if got := deployment.Labels["micro.dev/kind"]; got != "agent" {
|
||||
t.Fatalf("micro.dev/kind label = %q, want agent", got)
|
||||
}
|
||||
container := deployment.Pod.Container
|
||||
if container.Image != "ghcr.io/acme/support-agent:v1" {
|
||||
t.Fatalf("image = %q", container.Image)
|
||||
}
|
||||
if got := container.Environment["MICRO_REGISTRY"]; got != "kubernetes" {
|
||||
t.Fatalf("MICRO_REGISTRY = %q, want kubernetes", got)
|
||||
}
|
||||
if got := container.Environment["MODEL"]; got != "gpt-5.5" {
|
||||
t.Fatalf("MODEL = %q, want gpt-5.5", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapDeploymentDefaultsAndValidation(t *testing.T) {
|
||||
deployment, err := MapDeployment(Resource{Kind: KindService, Name: "api", Spec: WorkloadSpec{Image: "api:latest"}})
|
||||
if err != nil {
|
||||
t.Fatalf("MapDeployment returned error: %v", err)
|
||||
}
|
||||
if deployment.Namespace != "default" || deployment.Replicas != 1 {
|
||||
t.Fatalf("defaults = namespace %q replicas %d", deployment.Namespace, deployment.Replicas)
|
||||
}
|
||||
|
||||
if _, err := MapDeployment(Resource{Kind: KindFlow, Name: "ingest"}); err == nil {
|
||||
t.Fatal("MapDeployment without image succeeded")
|
||||
}
|
||||
if _, err := MapDeployment(Resource{Kind: "Job", Name: "job", Spec: WorkloadSpec{Image: "job:latest"}}); err == nil {
|
||||
t.Fatal("MapDeployment with unsupported kind succeeded")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package kubernetes
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// crdFS holds the canonical CRD manifests. They live as real YAML under
|
||||
// config/crd/ so they can be applied directly (`kubectl apply -f
|
||||
// deploy/kubernetes/config/crd/`) and are embedded here so the Go API serves
|
||||
// the exact same bytes — one source of truth, no drift.
|
||||
//
|
||||
//go:embed config/crd/agent.yaml config/crd/service.yaml config/crd/flow.yaml
|
||||
var crdFS embed.FS
|
||||
|
||||
// CRDManifests contains the alpha CRDs for Go Micro lifecycle resources, loaded
|
||||
// from the embedded config/crd/ YAML.
|
||||
var CRDManifests = map[Kind]string{
|
||||
KindAgent: mustCRD("agent"),
|
||||
KindService: mustCRD("service"),
|
||||
KindFlow: mustCRD("flow"),
|
||||
}
|
||||
|
||||
// mustCRD reads an embedded CRD manifest. The files are embedded at compile
|
||||
// time, so a read error means a build/packaging bug, not a runtime condition.
|
||||
func mustCRD(name string) string {
|
||||
b, err := crdFS.ReadFile("config/crd/" + name + ".yaml")
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("kubernetes: embedded CRD %q missing: %v", name, err))
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package kubernetes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
// Group is the API group for the alpha Go Micro Kubernetes resources.
|
||||
Group = "micro.dev"
|
||||
// Version is the current alpha API version for the CRDs in this package.
|
||||
Version = "v1alpha1"
|
||||
)
|
||||
|
||||
// Kind identifies a Go Micro lifecycle resource that can be reconciled toward a
|
||||
// Kubernetes Deployment.
|
||||
type Kind string
|
||||
|
||||
const (
|
||||
KindAgent Kind = "Agent"
|
||||
KindService Kind = "Service"
|
||||
KindFlow Kind = "Flow"
|
||||
)
|
||||
|
||||
// WorkloadSpec is the common alpha spec shared by Agent, Service, and Flow CRDs.
|
||||
type WorkloadSpec struct {
|
||||
Image string `json:"image"`
|
||||
Command []string `json:"command,omitempty"`
|
||||
Args []string `json:"args,omitempty"`
|
||||
Replicas int32 `json:"replicas,omitempty"`
|
||||
Registry string `json:"registry,omitempty"`
|
||||
Environment map[string]string `json:"env,omitempty"`
|
||||
}
|
||||
|
||||
// Resource is the minimal desired state for a Go Micro lifecycle resource.
|
||||
type Resource struct {
|
||||
Kind Kind
|
||||
Name string
|
||||
Namespace string
|
||||
Spec WorkloadSpec
|
||||
}
|
||||
|
||||
// Deployment is a small, dependency-free representation of the Kubernetes
|
||||
// Deployment fields the alpha reconciler skeleton owns.
|
||||
type Deployment struct {
|
||||
Name string
|
||||
Namespace string
|
||||
Labels map[string]string
|
||||
Replicas int32
|
||||
Pod PodTemplate
|
||||
}
|
||||
|
||||
// PodTemplate describes the pod fields emitted by MapDeployment.
|
||||
type PodTemplate struct {
|
||||
Labels map[string]string
|
||||
Container Container
|
||||
}
|
||||
|
||||
// Container describes the single Go Micro workload container.
|
||||
type Container struct {
|
||||
Name string
|
||||
Image string
|
||||
Command []string
|
||||
Args []string
|
||||
Environment map[string]string
|
||||
}
|
||||
|
||||
// MapDeployment maps a Go Micro alpha resource to the Deployment shape an
|
||||
// operator reconciliation loop would apply.
|
||||
func MapDeployment(resource Resource) (Deployment, error) {
|
||||
if resource.Kind != KindAgent && resource.Kind != KindService && resource.Kind != KindFlow {
|
||||
return Deployment{}, fmt.Errorf("unsupported kind %q", resource.Kind)
|
||||
}
|
||||
name := strings.TrimSpace(resource.Name)
|
||||
if name == "" {
|
||||
return Deployment{}, fmt.Errorf("name is required")
|
||||
}
|
||||
image := strings.TrimSpace(resource.Spec.Image)
|
||||
if image == "" {
|
||||
return Deployment{}, fmt.Errorf("spec.image is required")
|
||||
}
|
||||
|
||||
namespace := strings.TrimSpace(resource.Namespace)
|
||||
if namespace == "" {
|
||||
namespace = "default"
|
||||
}
|
||||
replicas := resource.Spec.Replicas
|
||||
if replicas == 0 {
|
||||
replicas = 1
|
||||
}
|
||||
|
||||
labels := map[string]string{
|
||||
"app.kubernetes.io/name": name,
|
||||
"app.kubernetes.io/managed-by": "go-micro",
|
||||
"micro.dev/kind": strings.ToLower(string(resource.Kind)),
|
||||
}
|
||||
env := copyMap(resource.Spec.Environment)
|
||||
if resource.Spec.Registry != "" {
|
||||
env["MICRO_REGISTRY"] = resource.Spec.Registry
|
||||
}
|
||||
|
||||
return Deployment{
|
||||
Name: name,
|
||||
Namespace: namespace,
|
||||
Labels: copyMap(labels),
|
||||
Replicas: replicas,
|
||||
Pod: PodTemplate{
|
||||
Labels: copyMap(labels),
|
||||
Container: Container{
|
||||
Name: name,
|
||||
Image: image,
|
||||
Command: append([]string(nil), resource.Spec.Command...),
|
||||
Args: append([]string(nil), resource.Spec.Args...),
|
||||
Environment: env,
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// EnvironmentKeys returns stable environment variable keys from a mapped
|
||||
// container. It is useful for deterministic validation and rendering.
|
||||
func (c Container) EnvironmentKeys() []string {
|
||||
keys := make([]string, 0, len(c.Environment))
|
||||
for key := range c.Environment {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
return keys
|
||||
}
|
||||
|
||||
func copyMap(in map[string]string) map[string]string {
|
||||
out := make(map[string]string, len(in))
|
||||
for k, v := range in {
|
||||
out[k] = v
|
||||
}
|
||||
return out
|
||||
}
|
||||
+4
-1
@@ -12,6 +12,7 @@ provider-free unless the example README says otherwise.
|
||||
| Prove the maintained 0→hero path | [`support`](./support/) | `go run ./examples/support` and `go test ./examples/support` | [`zero-to-hero` guide](../internal/website/docs/guides/zero-to-hero.md) |
|
||||
| See planning and delegation | [`agent-plan-delegate`](./agent-plan-delegate/) | `go run ./examples/agent-plan-delegate` | [`plan-delegate` guide](../internal/website/docs/guides/plan-delegate.md) |
|
||||
| Expose services through MCP | [`mcp/hello`](./mcp/hello/) | follow [`mcp`](./mcp/) setup | [`mcp/crud`](./mcp/crud/) and [`mcp/workflow`](./mcp/workflow/) |
|
||||
| Try a paid tool with x402 | [`agent-x402-buyer`](./agent-x402-buyer/) | `go run ./examples/agent-x402-buyer` | [`Payments (x402)` guide](../internal/website/docs/guides/x402-payments.md) |
|
||||
| Try A2A or gRPC interop next | [`agent-demo`](./agent-demo/) plus gateway docs | run the example, then use the gateway docs | [`grpc-interop`](./grpc-interop/) |
|
||||
| Add workflow durability | [`flow-durable`](./flow-durable/) | `go run ./examples/flow-durable` | [`flow-loop`](./flow-loop/) |
|
||||
|
||||
@@ -28,7 +29,9 @@ provider-free unless the example README says otherwise.
|
||||
4. **Interop next:** use [`mcp/hello`](./mcp/hello/), [`mcp/crud`](./mcp/crud/),
|
||||
and [`mcp/workflow`](./mcp/workflow/) when you are ready to expose tools to
|
||||
external AI clients.
|
||||
5. **Workflow depth:** use [`flow-durable`](./flow-durable/) once the agent path
|
||||
5. **Paid tools:** run [`agent-x402-buyer`](./agent-x402-buyer/) to see an
|
||||
agent pay a local x402-protected tool with a mock facilitator and budget.
|
||||
6. **Workflow depth:** use [`flow-durable`](./flow-durable/) once the agent path
|
||||
needs checkpointed, resumable deterministic work.
|
||||
|
||||
## CLI wayfinding
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
# Agent x402 buyer
|
||||
|
||||
This example shows an agent paying for a paid HTTP tool with x402 without using
|
||||
live funds or a live chain.
|
||||
|
||||
It starts a local paid endpoint guarded by `wrapper/x402` seller middleware and a
|
||||
mock facilitator. A deterministic mock-model agent calls that endpoint as a tool,
|
||||
receives the HTTP 402 challenge, pays with `AgentPayer`, stays inside
|
||||
`AgentBudget`, retries the request, and prints the spend recorded for the run.
|
||||
|
||||
```bash
|
||||
go run ./examples/agent-x402-buyer
|
||||
```
|
||||
|
||||
Expected output includes:
|
||||
|
||||
- the paid tool response,
|
||||
- one facilitator verify and settle call, and
|
||||
- `run spend: 7 smallest units (budget 10)`.
|
||||
|
||||
The payment token and facilitator are intentionally local development fakes. To
|
||||
settle real x402 payments, keep the same `AgentPayer` / `AgentBudget` shape but
|
||||
replace the payer with a wallet-backed implementation and configure the seller
|
||||
middleware with a hosted or self-run x402 facilitator.
|
||||
@@ -0,0 +1,168 @@
|
||||
// Agent x402 buyer — a provider-free example of an agent paying for a paid tool.
|
||||
//
|
||||
// Run:
|
||||
//
|
||||
// go run ./examples/agent-x402-buyer
|
||||
//
|
||||
// It starts a local HTTP tool protected by x402 middleware, then asks a
|
||||
// deterministic mock-model agent to call that tool. The agent receives the 402
|
||||
// challenge, uses AgentPayer and AgentBudget to pay within a local mock
|
||||
// facilitator, retries the request, and prints the run spend.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
go_micro "go-micro.dev/v6"
|
||||
"go-micro.dev/v6/agent"
|
||||
"go-micro.dev/v6/ai"
|
||||
"go-micro.dev/v6/store"
|
||||
"go-micro.dev/v6/wrapper/x402"
|
||||
)
|
||||
|
||||
const (
|
||||
paidToolName = "paid.market_brief"
|
||||
price = int64(7)
|
||||
paymentToken = "dev-payment-token"
|
||||
)
|
||||
|
||||
type devFacilitator struct {
|
||||
verifyCount int
|
||||
settleCount int
|
||||
}
|
||||
|
||||
func (f *devFacilitator) Verify(ctx context.Context, payment string, req x402.Requirements) (x402.Result, error) {
|
||||
f.verifyCount++
|
||||
if payment != paymentToken {
|
||||
return x402.Result{Valid: false, Reason: "unknown dev payment token"}, nil
|
||||
}
|
||||
return x402.Result{Valid: true, Payer: "dev-agent-wallet"}, nil
|
||||
}
|
||||
|
||||
func (f *devFacilitator) Settle(ctx context.Context, payment string, req x402.Requirements) (x402.Result, error) {
|
||||
f.settleCount++
|
||||
return x402.Result{Valid: true, Settlement: "dev-settlement-001"}, nil
|
||||
}
|
||||
|
||||
type devPayer struct{}
|
||||
|
||||
func (devPayer) Pay(ctx context.Context, req x402.Requirements) (string, error) {
|
||||
return paymentToken, nil
|
||||
}
|
||||
|
||||
type mockModel struct{ opts ai.Options }
|
||||
|
||||
func newMock(opts ...ai.Option) ai.Model {
|
||||
m := &mockModel{}
|
||||
_ = m.Init(opts...)
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *mockModel) Init(opts ...ai.Option) error {
|
||||
for _, o := range opts {
|
||||
o(&m.opts)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (m *mockModel) Options() ai.Options { return m.opts }
|
||||
func (m *mockModel) String() string { return "agent-x402-buyer-mock" }
|
||||
func (m *mockModel) Stream(context.Context, *ai.Request, ...ai.GenerateOption) (ai.Stream, error) {
|
||||
return nil, fmt.Errorf("stream not supported by agent-x402-buyer mock")
|
||||
}
|
||||
|
||||
func (m *mockModel) Generate(ctx context.Context, req *ai.Request, _ ...ai.GenerateOption) (*ai.Response, error) {
|
||||
for _, tool := range req.Tools {
|
||||
if tool.Name == paidToolName && m.opts.ToolHandler != nil {
|
||||
out := m.opts.ToolHandler(ctx, ai.ToolCall{ID: "paid-brief", Name: tool.Name, Input: map[string]any{"url": req.Prompt}})
|
||||
return &ai.Response{Answer: fmt.Sprintf("Paid tool returned: %s", out.Content)}, nil
|
||||
}
|
||||
}
|
||||
return &ai.Response{Answer: "No paid tool was available."}, nil
|
||||
}
|
||||
|
||||
func paidToolServer(fac *devFacilitator) *httptest.Server {
|
||||
mux := http.NewServeMux()
|
||||
paid := x402.Middleware(x402.Config{
|
||||
PayTo: "0xMerchantDevWallet",
|
||||
Network: "base-sepolia",
|
||||
Amount: fmt.Sprint(price),
|
||||
Description: "Local market brief for the x402 buyer example",
|
||||
Facilitator: fac,
|
||||
})
|
||||
mux.Handle("/brief", paid(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"brief": "Mock demand is up 12% after the agent paid the local tool.",
|
||||
"settlement": w.Header().Get(x402.PaymentResponseHeader),
|
||||
})
|
||||
})))
|
||||
return httptest.NewServer(mux)
|
||||
}
|
||||
|
||||
func run(w io.Writer) error {
|
||||
ai.Register("agent-x402-buyer-mock", newMock)
|
||||
|
||||
fac := &devFacilitator{}
|
||||
srv := paidToolServer(fac)
|
||||
defer srv.Close()
|
||||
|
||||
st := store.NewMemoryStore()
|
||||
buyer := agent.New(
|
||||
agent.Name("x402-buyer"),
|
||||
agent.Provider("agent-x402-buyer-mock"),
|
||||
agent.Prompt("Call the paid market brief tool when given its URL."),
|
||||
agent.WithStore(st),
|
||||
go_micro.AgentPayer(devPayer{}),
|
||||
go_micro.AgentBudget(10),
|
||||
agent.WithTool(paidToolName, "Fetch a paid market brief over HTTP", map[string]any{
|
||||
"url": map[string]any{"type": "string", "description": "Paid HTTP endpoint to call"},
|
||||
}, func(ctx context.Context, input map[string]any) (string, error) {
|
||||
url, _ := input["url"].(string)
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(body), nil
|
||||
}),
|
||||
)
|
||||
|
||||
resp, err := buyer.Ask(context.Background(), srv.URL+"/brief")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
events, err := agent.LoadRunEvents(st, "x402-buyer", resp.RunID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var spent int64
|
||||
for _, event := range events {
|
||||
if event.Spent > spent {
|
||||
spent = event.Spent
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Fprintln(w, "Agent x402 buyer (provider: mock, funds: local dev token)")
|
||||
fmt.Fprintln(w, strings.TrimSpace(resp.Reply))
|
||||
fmt.Fprintf(w, "facilitator verify=%d settle=%d\n", fac.verifyCount, fac.settleCount)
|
||||
fmt.Fprintf(w, "run spend: %d smallest units (budget 10)\n", spent)
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := run(os.Stdout); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
+101
-39
@@ -108,6 +108,9 @@ func NewAgentHandler(card AgentCard, invoke Invoke) http.Handler {
|
||||
card.URL = strings.TrimRight(card.URL, "/")
|
||||
serveCard := func(w http.ResponseWriter, _ *http.Request) { writeJSON(w, http.StatusOK, card) }
|
||||
mux.HandleFunc("GET /{$}", serveCard)
|
||||
// A2A 0.3.0 discovery is /.well-known/agent-card.json; agent.json is the
|
||||
// pre-0.3 alias, kept so existing clients don't break.
|
||||
mux.HandleFunc("GET /.well-known/agent-card.json", serveCard)
|
||||
mux.HandleFunc("GET /.well-known/agent.json", serveCard)
|
||||
mux.HandleFunc("POST /{$}", func(w http.ResponseWriter, r *http.Request) { d.serve(w, r, invoke) })
|
||||
return mux
|
||||
@@ -121,6 +124,7 @@ func NewAgentStreamHandler(card AgentCard, invoke Invoke, stream StreamInvoke) h
|
||||
card.URL = strings.TrimRight(card.URL, "/")
|
||||
serveCard := func(w http.ResponseWriter, _ *http.Request) { writeJSON(w, http.StatusOK, card) }
|
||||
mux.HandleFunc("GET /{$}", serveCard)
|
||||
mux.HandleFunc("GET /.well-known/agent-card.json", serveCard)
|
||||
mux.HandleFunc("GET /.well-known/agent.json", serveCard)
|
||||
mux.HandleFunc("POST /{$}", func(w http.ResponseWriter, r *http.Request) { d.serveWithStream(w, r, invoke, stream) })
|
||||
return mux
|
||||
@@ -139,15 +143,19 @@ func (g *Gateway) Handler() http.Handler {
|
||||
// Discovery: a directory of all agent cards.
|
||||
mux.HandleFunc("GET /agents", g.handleList)
|
||||
// Per-agent card (served at the agent's url and at its well-known path).
|
||||
// A2A 0.3.0 uses agent-card.json; agent.json is the pre-0.3 alias.
|
||||
mux.HandleFunc("GET /agents/{name}", g.handleCard)
|
||||
mux.HandleFunc("GET /agents/{name}/.well-known/agent-card.json", g.handleCard)
|
||||
mux.HandleFunc("GET /agents/{name}/.well-known/agent.json", g.handleCard)
|
||||
mux.HandleFunc("GET /agents/{name}/skills/{skill}", g.handleSkillCard)
|
||||
mux.HandleFunc("GET /agents/{name}/skills/{skill}/.well-known/agent-card.json", g.handleSkillCard)
|
||||
mux.HandleFunc("GET /agents/{name}/skills/{skill}/.well-known/agent.json", g.handleSkillCard)
|
||||
// Per-agent JSON-RPC endpoint.
|
||||
mux.HandleFunc("POST /agents/{name}", g.handleRPC)
|
||||
mux.HandleFunc("POST /agents/{name}/skills/{skill}", g.handleSkillRPC)
|
||||
// Top-level well-known: serve the single agent's card if there's
|
||||
// exactly one, otherwise point to the directory.
|
||||
mux.HandleFunc("GET /.well-known/agent-card.json", g.handleWellKnown)
|
||||
mux.HandleFunc("GET /.well-known/agent.json", g.handleWellKnown)
|
||||
return mux
|
||||
}
|
||||
@@ -222,6 +230,39 @@ type Artifact struct {
|
||||
Parts []Part `json:"parts"`
|
||||
}
|
||||
|
||||
// TaskStatusUpdateEvent is an A2A streaming event reporting a change in a
|
||||
// task's status. External SSE clients parse stream events by `kind` and stop
|
||||
// on the event whose `final` is true — a full Task snapshot (which older
|
||||
// versions emitted) carries neither, so strict clients never terminate.
|
||||
type TaskStatusUpdateEvent struct {
|
||||
TaskID string `json:"taskId"`
|
||||
ContextID string `json:"contextId"`
|
||||
Kind string `json:"kind"` // "status-update"
|
||||
Status TaskStatus `json:"status"`
|
||||
Final bool `json:"final"`
|
||||
}
|
||||
|
||||
// TaskArtifactUpdateEvent is an A2A streaming event carrying an artifact (or,
|
||||
// with Append, one incremental chunk of one).
|
||||
type TaskArtifactUpdateEvent struct {
|
||||
TaskID string `json:"taskId"`
|
||||
ContextID string `json:"contextId"`
|
||||
Kind string `json:"kind"` // "artifact-update"
|
||||
Artifact Artifact `json:"artifact"`
|
||||
Append bool `json:"append,omitempty"`
|
||||
LastChunk bool `json:"lastChunk,omitempty"`
|
||||
}
|
||||
|
||||
func statusUpdateEvent(t *Task, final bool) TaskStatusUpdateEvent {
|
||||
return TaskStatusUpdateEvent{
|
||||
TaskID: t.ID,
|
||||
ContextID: t.ContextID,
|
||||
Kind: "status-update",
|
||||
Status: t.Status,
|
||||
Final: final,
|
||||
}
|
||||
}
|
||||
|
||||
// Task is the unit of work returned by message/send and tasks/get.
|
||||
type Task struct {
|
||||
ID string `json:"id"`
|
||||
@@ -534,14 +575,11 @@ func (d *dispatcher) stream(ctx context.Context, w http.ResponseWriter, req rpcR
|
||||
writeRPC(w, req.ID, nil, e)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
w.Header().Set("Connection", "keep-alive")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_ = json.NewEncoder(sseWriter{w: w}).Encode(rpcResponse{JSONRPC: "2.0", ID: req.ID, Result: task})
|
||||
if f, ok := w.(http.Flusher); ok {
|
||||
f.Flush()
|
||||
}
|
||||
enc, flush := sseResponse(w)
|
||||
// The Task snapshot first (carries ids and the final artifact), then a
|
||||
// terminal status-update so external SSE clients see `final:true` and stop.
|
||||
writeSSE(enc, flush, req.ID, task)
|
||||
writeSSE(enc, flush, req.ID, statusUpdateEvent(task, true))
|
||||
}
|
||||
|
||||
func (d *dispatcher) streamChunks(ctx context.Context, w http.ResponseWriter, req rpcRequest, invoke StreamInvoke, fallback Invoke) {
|
||||
@@ -565,46 +603,53 @@ func (d *dispatcher) streamChunks(ctx context.Context, w http.ResponseWriter, re
|
||||
return
|
||||
}
|
||||
defer stream.Close()
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
w.Header().Set("Connection", "keep-alive")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
enc := json.NewEncoder(sseWriter{w: w})
|
||||
flush := func() {
|
||||
if f, ok := w.(http.Flusher); ok {
|
||||
f.Flush()
|
||||
}
|
||||
}
|
||||
enc, flush := sseResponse(w)
|
||||
taskID := uuid.New().String()
|
||||
contextID := p.Message.ContextID
|
||||
if contextID == "" {
|
||||
contextID = uuid.New().String()
|
||||
}
|
||||
// One artifact id for the whole stream so append:true chunks target it.
|
||||
artifactID := uuid.New().String()
|
||||
|
||||
// Open with the Task snapshot (working) so the client learns the ids.
|
||||
initial := taskFromReplyWithIDs(p.Message, "", stateWorking, taskID, contextID)
|
||||
d.store(initial)
|
||||
writeSSE(enc, flush, req.ID, initial)
|
||||
|
||||
var reply strings.Builder
|
||||
for {
|
||||
chunk, err := stream.Recv()
|
||||
if err == io.EOF {
|
||||
task := taskFromReplyWithIDs(p.Message, reply.String(), stateCompleted, taskID, contextID)
|
||||
d.store(task)
|
||||
_ = enc.Encode(rpcResponse{JSONRPC: "2.0", ID: req.ID, Result: task})
|
||||
flush()
|
||||
// Spec-shaped terminal: a status-update with final:true — not a
|
||||
// full Task snapshot, which carries no terminal marker.
|
||||
writeSSE(enc, flush, req.ID, statusUpdateEvent(task, true))
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
task := taskFromReplyWithIDs(p.Message, "error: "+err.Error(), stateFailed, taskID, contextID)
|
||||
d.store(task)
|
||||
_ = enc.Encode(rpcResponse{JSONRPC: "2.0", ID: req.ID, Result: task, Error: &rpcError{Code: errInternal, Message: err.Error()}})
|
||||
flush()
|
||||
// A failed status-update (final) — never `result` and `error`
|
||||
// together in one response, which strict clients reject.
|
||||
writeSSE(enc, flush, req.ID, statusUpdateEvent(task, true))
|
||||
return
|
||||
}
|
||||
if chunk == nil || chunk.Reply == "" {
|
||||
continue
|
||||
}
|
||||
reply.WriteString(chunk.Reply)
|
||||
task := taskFromReplyWithIDs(p.Message, reply.String(), stateWorking, taskID, contextID)
|
||||
d.store(task)
|
||||
_ = enc.Encode(rpcResponse{JSONRPC: "2.0", ID: req.ID, Result: task})
|
||||
flush()
|
||||
// Emit the delta as an append artifact-update; keep the stored task
|
||||
// current for tasks/get and resubscribe watchers.
|
||||
d.store(taskFromReplyWithIDs(p.Message, reply.String(), stateWorking, taskID, contextID))
|
||||
writeSSE(enc, flush, req.ID, TaskArtifactUpdateEvent{
|
||||
TaskID: taskID,
|
||||
ContextID: contextID,
|
||||
Kind: "artifact-update",
|
||||
Artifact: Artifact{ArtifactID: artifactID, Parts: []Part{{Kind: "text", Text: chunk.Reply}}},
|
||||
Append: true,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -653,20 +698,16 @@ func (d *dispatcher) resubscribe(ctx context.Context, w http.ResponseWriter, req
|
||||
}
|
||||
defer unsubscribe()
|
||||
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
w.Header().Set("Connection", "keep-alive")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
enc := json.NewEncoder(sseWriter{w: w})
|
||||
flush := func() {
|
||||
if f, ok := w.(http.Flusher); ok {
|
||||
f.Flush()
|
||||
}
|
||||
}
|
||||
enc, flush := sseResponse(w)
|
||||
writeEvent := func(t *Task) bool {
|
||||
_ = enc.Encode(rpcResponse{JSONRPC: "2.0", ID: req.ID, Result: t})
|
||||
flush()
|
||||
return isTerminal(t.Status.State)
|
||||
writeSSE(enc, flush, req.ID, t)
|
||||
if isTerminal(t.Status.State) {
|
||||
// Close the stream with a spec-shaped terminal marker so external
|
||||
// clients see `final:true`.
|
||||
writeSSE(enc, flush, req.ID, statusUpdateEvent(t, true))
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
if writeEvent(task) {
|
||||
return
|
||||
@@ -1029,6 +1070,27 @@ func requestContext(parent context.Context) context.Context {
|
||||
return ctx
|
||||
}
|
||||
|
||||
// sseResponse writes the SSE response headers and returns an encoder and a
|
||||
// flush func for emitting `data:`-framed JSON-RPC events.
|
||||
func sseResponse(w http.ResponseWriter) (*json.Encoder, func()) {
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
w.Header().Set("Connection", "keep-alive")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
enc := json.NewEncoder(sseWriter{w: w})
|
||||
return enc, func() {
|
||||
if f, ok := w.(http.Flusher); ok {
|
||||
f.Flush()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// writeSSE emits one JSON-RPC event (result only — never with an error) and flushes.
|
||||
func writeSSE(enc *json.Encoder, flush func(), id json.RawMessage, result any) {
|
||||
_ = enc.Encode(rpcResponse{JSONRPC: "2.0", ID: id, Result: result})
|
||||
flush()
|
||||
}
|
||||
|
||||
type sseWriter struct {
|
||||
w http.ResponseWriter
|
||||
}
|
||||
|
||||
+196
-100
@@ -99,6 +99,38 @@ func TestAgentCardFromRegistry(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A2A 0.3.0 discovery is /.well-known/agent-card.json. The card must be
|
||||
// reachable there (canonical) as well as at the legacy agent.json alias, both
|
||||
// per-agent and at the single-agent top level.
|
||||
func TestAgentCardCanonicalWellKnownPath(t *testing.T) {
|
||||
ts, cleanup := newGatewayWithAgent(t)
|
||||
defer cleanup()
|
||||
|
||||
for _, path := range []string{
|
||||
"/agents/echo/.well-known/agent-card.json",
|
||||
"/agents/echo/.well-known/agent.json",
|
||||
"/agents/echo/skills/task/.well-known/agent-card.json",
|
||||
} {
|
||||
resp, err := http.Get(ts.URL + path)
|
||||
if err != nil {
|
||||
t.Fatalf("get %s: %v", path, err)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
resp.Body.Close()
|
||||
t.Fatalf("%s status = %d, want 200", path, resp.StatusCode)
|
||||
}
|
||||
var card AgentCard
|
||||
if err := json.NewDecoder(resp.Body).Decode(&card); err != nil {
|
||||
resp.Body.Close()
|
||||
t.Fatalf("%s decode card: %v", path, err)
|
||||
}
|
||||
resp.Body.Close()
|
||||
if card.Name != "echo" {
|
||||
t.Errorf("%s card name = %q, want echo", path, card.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSkillEndpointServesFocusedCardAndRoutesRPC(t *testing.T) {
|
||||
ts, cleanup := newGatewayWithAgent(t)
|
||||
defer cleanup()
|
||||
@@ -335,6 +367,67 @@ func (s *sliceStream) Recv() (*ai.Response, error) {
|
||||
|
||||
func (s *sliceStream) Close() error { return nil }
|
||||
|
||||
// streamEvent is one decoded SSE JSON-RPC event from a message/stream response.
|
||||
// A2A streams carry heterogeneous results (Task, status-update, artifact-update)
|
||||
// discriminated by `kind`, so we keep the raw result and decode on demand.
|
||||
type streamEvent struct {
|
||||
Result json.RawMessage `json:"result"`
|
||||
Error *rpcError `json:"error"`
|
||||
}
|
||||
|
||||
func (e streamEvent) kind() string {
|
||||
var k struct {
|
||||
Kind string `json:"kind"`
|
||||
}
|
||||
_ = json.Unmarshal(e.Result, &k)
|
||||
return k.Kind
|
||||
}
|
||||
|
||||
func (e streamEvent) task(t *testing.T) Task {
|
||||
t.Helper()
|
||||
var task Task
|
||||
if err := json.Unmarshal(e.Result, &task); err != nil {
|
||||
t.Fatalf("decode task event: %v", err)
|
||||
}
|
||||
return task
|
||||
}
|
||||
|
||||
func (e streamEvent) status(t *testing.T) TaskStatusUpdateEvent {
|
||||
t.Helper()
|
||||
var s TaskStatusUpdateEvent
|
||||
if err := json.Unmarshal(e.Result, &s); err != nil {
|
||||
t.Fatalf("decode status-update event: %v", err)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (e streamEvent) artifactUpdate(t *testing.T) TaskArtifactUpdateEvent {
|
||||
t.Helper()
|
||||
var a TaskArtifactUpdateEvent
|
||||
if err := json.Unmarshal(e.Result, &a); err != nil {
|
||||
t.Fatalf("decode artifact-update event: %v", err)
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// collectSSE parses the `data:`-framed JSON-RPC events from an SSE body.
|
||||
func collectSSE(t *testing.T, body string) []streamEvent {
|
||||
t.Helper()
|
||||
var events []streamEvent
|
||||
for _, line := range strings.Split(strings.TrimSpace(body), "\n") {
|
||||
line = strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(line), "data:"))
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
var e streamEvent
|
||||
if err := json.Unmarshal([]byte(line), &e); err != nil {
|
||||
t.Fatalf("decode event %q: %v", line, err)
|
||||
}
|
||||
events = append(events, e)
|
||||
}
|
||||
return events
|
||||
}
|
||||
|
||||
func TestMessageStreamChunksStoreFinalTask(t *testing.T) {
|
||||
d := newDispatcher()
|
||||
body := `{"jsonrpc":"2.0","id":1,"method":"message/stream","params":{"message":{"role":"user","parts":[{"kind":"text","text":"ping"}],"kind":"message"}}}`
|
||||
@@ -351,47 +444,61 @@ func TestMessageStreamChunksStoreFinalTask(t *testing.T) {
|
||||
if ct := rr.Result().Header.Get("Content-Type"); !strings.HasPrefix(ct, "text/event-stream") {
|
||||
t.Fatalf("content-type = %q, want text/event-stream", ct)
|
||||
}
|
||||
var events []struct {
|
||||
Result Task `json:"result"`
|
||||
Error *rpcError `json:"error"`
|
||||
events := collectSSE(t, rr.Body.String())
|
||||
// Opening Task snapshot + one append artifact-update per chunk + terminal
|
||||
// status-update.
|
||||
if len(events) != 4 {
|
||||
t.Fatalf("events = %d, want 4; body %s", len(events), rr.Body.String())
|
||||
}
|
||||
for _, line := range strings.Split(strings.TrimSpace(rr.Body.String()), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
line = strings.TrimPrefix(line, "data: ")
|
||||
var event struct {
|
||||
Result Task `json:"result"`
|
||||
Error *rpcError `json:"error"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(line), &event); err != nil {
|
||||
t.Fatalf("decode event %q: %v", line, err)
|
||||
}
|
||||
events = append(events, event)
|
||||
}
|
||||
if len(events) != 3 {
|
||||
t.Fatalf("events = %d, want 3; body %s", len(events), rr.Body.String())
|
||||
}
|
||||
for i, event := range events {
|
||||
if event.Error != nil {
|
||||
t.Fatalf("event %d error: %+v", i, event.Error)
|
||||
}
|
||||
if event.Result.ID != events[0].Result.ID || event.Result.ContextID != events[0].Result.ContextID {
|
||||
t.Fatalf("event %d changed task identity: %+v vs %+v", i, event.Result, events[0].Result)
|
||||
for i, e := range events {
|
||||
if e.Error != nil {
|
||||
t.Fatalf("event %d carried an error field: %+v", i, e.Error)
|
||||
}
|
||||
}
|
||||
if events[0].Result.Status.State != stateWorking || textOf(events[0].Result.Artifacts[0].Parts) != "po" {
|
||||
t.Fatalf("first event = %+v, want working po", events[0].Result)
|
||||
if events[0].kind() != "task" {
|
||||
t.Fatalf("first event kind = %q, want task", events[0].kind())
|
||||
}
|
||||
final := events[len(events)-1].Result
|
||||
if final.Status.State != stateCompleted || textOf(final.Artifacts[0].Parts) != "pong" {
|
||||
t.Fatalf("final event = %+v, want completed pong", final)
|
||||
opening := events[0].task(t)
|
||||
if opening.Status.State != stateWorking {
|
||||
t.Fatalf("opening task state = %q, want working", opening.Status.State)
|
||||
}
|
||||
taskID := opening.ID
|
||||
|
||||
// The middle events are append artifact-updates carrying the chunk deltas.
|
||||
var text strings.Builder
|
||||
for _, e := range events[1:3] {
|
||||
if e.kind() != "artifact-update" {
|
||||
t.Fatalf("event kind = %q, want artifact-update", e.kind())
|
||||
}
|
||||
au := e.artifactUpdate(t)
|
||||
if !au.Append {
|
||||
t.Fatalf("artifact-update should be append: %+v", au)
|
||||
}
|
||||
if au.TaskID != taskID {
|
||||
t.Fatalf("artifact-update taskId = %q, want %q", au.TaskID, taskID)
|
||||
}
|
||||
text.WriteString(textOf(au.Artifact.Parts))
|
||||
}
|
||||
if text.String() != "pong" {
|
||||
t.Fatalf("accumulated artifact text = %q, want pong", text.String())
|
||||
}
|
||||
|
||||
got := rpcTaskFromDispatcher(t, d, final.ID)
|
||||
if got.ID != final.ID || got.Status.State != stateCompleted || textOf(got.Artifacts[0].Parts) != "pong" {
|
||||
t.Fatalf("stored task = %+v, want final", got)
|
||||
// The stream closes with a terminal status-update (final:true).
|
||||
last := events[len(events)-1]
|
||||
if last.kind() != "status-update" {
|
||||
t.Fatalf("last event kind = %q, want status-update", last.kind())
|
||||
}
|
||||
su := last.status(t)
|
||||
if !su.Final || su.Status.State != stateCompleted {
|
||||
t.Fatalf("terminal event = %+v, want final completed", su)
|
||||
}
|
||||
if su.TaskID != taskID {
|
||||
t.Fatalf("terminal taskId = %q, want %q", su.TaskID, taskID)
|
||||
}
|
||||
|
||||
got := rpcTaskFromDispatcher(t, d, taskID)
|
||||
if got.ID != taskID || got.Status.State != stateCompleted || textOf(got.Artifacts[0].Parts) != "pong" {
|
||||
t.Fatalf("stored task = %+v, want final completed pong", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -432,37 +539,31 @@ func TestMessageStreamChunksPropagatesCancellationAndClosesStream(t *testing.T)
|
||||
t.Fatal("stream was not closed")
|
||||
}
|
||||
|
||||
var events []struct {
|
||||
Result Task `json:"result"`
|
||||
Error *rpcError `json:"error"`
|
||||
events := collectSSE(t, rr.Body.String())
|
||||
// Opening Task snapshot, then a terminal failed status-update.
|
||||
if len(events) != 2 {
|
||||
t.Fatalf("events = %d, want 2; body %s", len(events), rr.Body.String())
|
||||
}
|
||||
for _, line := range strings.Split(strings.TrimSpace(rr.Body.String()), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" {
|
||||
continue
|
||||
// A streaming failure must be a failed status-update, never `result` and
|
||||
// `error` set together in one response.
|
||||
for i, e := range events {
|
||||
if e.Error != nil {
|
||||
t.Fatalf("event %d carried an error field (result+error not allowed): %+v", i, e.Error)
|
||||
}
|
||||
line = strings.TrimPrefix(line, "data: ")
|
||||
var event struct {
|
||||
Result Task `json:"result"`
|
||||
Error *rpcError `json:"error"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(line), &event); err != nil {
|
||||
t.Fatalf("decode event %q: %v", line, err)
|
||||
}
|
||||
events = append(events, event)
|
||||
}
|
||||
if len(events) != 1 {
|
||||
t.Fatalf("events = %d, want 1; body %s", len(events), rr.Body.String())
|
||||
if events[0].kind() != "task" || events[0].task(t).Status.State != stateWorking {
|
||||
t.Fatalf("first event = %s, want working task", string(events[0].Result))
|
||||
}
|
||||
event := events[0]
|
||||
if event.Error == nil || event.Error.Code != errInternal || event.Error.Message != context.Canceled.Error() {
|
||||
t.Fatalf("error = %+v, want context cancellation", event.Error)
|
||||
last := events[1]
|
||||
if last.kind() != "status-update" {
|
||||
t.Fatalf("last event kind = %q, want status-update", last.kind())
|
||||
}
|
||||
if event.Result.Status.State != stateFailed || textOf(event.Result.Artifacts[0].Parts) != "error: context canceled" {
|
||||
t.Fatalf("failed task = %+v, want context cancellation artifact", event.Result)
|
||||
su := last.status(t)
|
||||
if !su.Final || su.Status.State != stateFailed {
|
||||
t.Fatalf("terminal event = %+v, want final failed", su)
|
||||
}
|
||||
|
||||
got := rpcTaskFromDispatcher(t, d, event.Result.ID)
|
||||
got := rpcTaskFromDispatcher(t, d, su.TaskID)
|
||||
if got.Status.State != stateFailed || textOf(got.Artifacts[0].Parts) != "error: context canceled" {
|
||||
t.Fatalf("stored task = %+v, want failed cancellation", got)
|
||||
}
|
||||
@@ -493,33 +594,24 @@ func TestMessageStreamChunksFallsBackWhenUnsupported(t *testing.T) {
|
||||
if ct := rr.Result().Header.Get("Content-Type"); !strings.HasPrefix(ct, "text/event-stream") {
|
||||
t.Fatalf("content-type = %q, want text/event-stream", ct)
|
||||
}
|
||||
var events []struct {
|
||||
Result Task `json:"result"`
|
||||
Error *rpcError `json:"error"`
|
||||
events := collectSSE(t, rr.Body.String())
|
||||
// The non-streaming fallback emits a completed Task snapshot then a terminal
|
||||
// status-update.
|
||||
if len(events) != 2 {
|
||||
t.Fatalf("events = %d, want 2; body %s", len(events), rr.Body.String())
|
||||
}
|
||||
for _, line := range strings.Split(strings.TrimSpace(rr.Body.String()), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" {
|
||||
continue
|
||||
for i, e := range events {
|
||||
if e.Error != nil {
|
||||
t.Fatalf("fallback event %d error: %+v", i, e.Error)
|
||||
}
|
||||
line = strings.TrimPrefix(line, "data: ")
|
||||
var event struct {
|
||||
Result Task `json:"result"`
|
||||
Error *rpcError `json:"error"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(line), &event); err != nil {
|
||||
t.Fatalf("decode event %q: %v", line, err)
|
||||
}
|
||||
events = append(events, event)
|
||||
}
|
||||
if len(events) != 1 {
|
||||
t.Fatalf("events = %d, want 1; body %s", len(events), rr.Body.String())
|
||||
task := events[0].task(t)
|
||||
if task.Status.State != stateCompleted || textOf(task.Artifacts[0].Parts) != "pong" {
|
||||
t.Fatalf("fallback task = %+v, want completed pong", task)
|
||||
}
|
||||
if events[0].Error != nil {
|
||||
t.Fatalf("fallback event error: %+v", events[0].Error)
|
||||
}
|
||||
if events[0].Result.Status.State != stateCompleted || textOf(events[0].Result.Artifacts[0].Parts) != "pong" {
|
||||
t.Fatalf("fallback task = %+v, want completed pong", events[0].Result)
|
||||
su := events[1].status(t)
|
||||
if !su.Final || su.Status.State != stateCompleted {
|
||||
t.Fatalf("terminal event = %+v, want final completed", su)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -535,30 +627,34 @@ func TestMessageStreamFallbackDoesNotCompleteWithEmptyText(t *testing.T) {
|
||||
return nil, fmt.Errorf("%w: test provider", ai.ErrStreamingUnsupported)
|
||||
})
|
||||
|
||||
var event struct {
|
||||
Result Task `json:"result"`
|
||||
Error *rpcError `json:"error"`
|
||||
}
|
||||
for _, line := range strings.Split(strings.TrimSpace(rr.Body.String()), "\n") {
|
||||
line = strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(line), "data: "))
|
||||
if line == "" {
|
||||
continue
|
||||
events := collectSSE(t, rr.Body.String())
|
||||
var task Task
|
||||
var foundTask bool
|
||||
for _, e := range events {
|
||||
if e.Error != nil {
|
||||
t.Fatalf("fallback event error: %+v", e.Error)
|
||||
}
|
||||
if err := json.Unmarshal([]byte(line), &event); err != nil {
|
||||
t.Fatalf("decode event %q: %v", line, err)
|
||||
if e.kind() == "task" {
|
||||
task = e.task(t)
|
||||
foundTask = true
|
||||
}
|
||||
}
|
||||
if event.Error != nil {
|
||||
t.Fatalf("fallback event error: %+v", event.Error)
|
||||
if !foundTask {
|
||||
t.Fatalf("no task event in stream; body %s", rr.Body.String())
|
||||
}
|
||||
if event.Result.Status.State != stateFailed {
|
||||
t.Fatalf("fallback state = %q, want failed", event.Result.Status.State)
|
||||
if task.Status.State != stateFailed {
|
||||
t.Fatalf("fallback state = %q, want failed", task.Status.State)
|
||||
}
|
||||
if got := textOf(event.Result.Artifacts[0].Parts); got == "" {
|
||||
t.Fatalf("fallback artifact text is empty: %+v", event.Result.Artifacts)
|
||||
if got := textOf(task.Artifacts[0].Parts); got == "" {
|
||||
t.Fatalf("fallback artifact text is empty: %+v", task.Artifacts)
|
||||
}
|
||||
if got := textOf(event.Result.History[len(event.Result.History)-1].Parts); got == "" {
|
||||
t.Fatalf("fallback history text is empty: %+v", event.Result.History)
|
||||
if got := textOf(task.History[len(task.History)-1].Parts); got == "" {
|
||||
t.Fatalf("fallback history text is empty: %+v", task.History)
|
||||
}
|
||||
// The stream still ends with a terminal marker.
|
||||
last := events[len(events)-1]
|
||||
if last.kind() != "status-update" || !last.status(t).Final {
|
||||
t.Fatalf("stream must end with a final status-update; got %s", string(last.Result))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,282 @@
|
||||
package mcp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
reflectionpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/reflect/protodesc"
|
||||
"google.golang.org/protobuf/reflect/protoreflect"
|
||||
"google.golang.org/protobuf/reflect/protoregistry"
|
||||
"google.golang.org/protobuf/types/descriptorpb"
|
||||
"google.golang.org/protobuf/types/dynamicpb"
|
||||
)
|
||||
|
||||
// ReflectedGRPCTarget describes an external gRPC server whose reflection
|
||||
// catalog should be exposed as MCP tools. It is intentionally opt-in: teams can
|
||||
// bridge existing reflected gRPC services without changing their servers or
|
||||
// registering them in go-micro.
|
||||
type ReflectedGRPCTarget struct {
|
||||
// Name prefixes generated tools. When empty, Address is sanitized and used.
|
||||
Name string
|
||||
// Address is the host:port of the reflected gRPC server.
|
||||
Address string
|
||||
// DialOptions customize the connection. If none are supplied, an insecure
|
||||
// transport is used for local/dev interoperability.
|
||||
DialOptions []grpc.DialOption
|
||||
// Timeout bounds reflection discovery and individual tool calls.
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
func (s *Server) discoverReflectedGRPC() error {
|
||||
for _, target := range s.opts.ReflectedGRPCTargets {
|
||||
if strings.TrimSpace(target.Address) == "" {
|
||||
continue
|
||||
}
|
||||
tools, err := s.reflectedGRPCTools(target)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, tool := range tools {
|
||||
s.tools[tool.Name] = tool
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) reflectedGRPCTools(target ReflectedGRPCTarget) ([]*Tool, error) {
|
||||
timeout := target.Timeout
|
||||
if timeout == 0 {
|
||||
timeout = 10 * time.Second
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(s.opts.Context, timeout)
|
||||
defer cancel()
|
||||
|
||||
dialOpts := target.DialOptions
|
||||
if len(dialOpts) == 0 {
|
||||
dialOpts = []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
|
||||
}
|
||||
conn, err := grpc.NewClient(target.Address, dialOpts...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("connect reflected grpc target %s: %w", target.Address, err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
files, services, err := loadReflectedFiles(ctx, conn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("reflect grpc target %s: %w", target.Address, err)
|
||||
}
|
||||
|
||||
prefix := target.Name
|
||||
if prefix == "" {
|
||||
prefix = sanitizeToolPart(target.Address)
|
||||
}
|
||||
|
||||
var out []*Tool
|
||||
for _, serviceName := range services {
|
||||
desc, err := files.FindDescriptorByName(protoreflect.FullName(serviceName))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
svc, ok := desc.(protoreflect.ServiceDescriptor)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
for i := 0; i < svc.Methods().Len(); i++ {
|
||||
method := svc.Methods().Get(i)
|
||||
if method.IsStreamingClient() || method.IsStreamingServer() {
|
||||
continue
|
||||
}
|
||||
fullMethod := "/" + string(svc.FullName()) + "/" + string(method.Name())
|
||||
toolName := prefix + "." + strings.ReplaceAll(string(svc.FullName()), ".", "_") + "." + string(method.Name())
|
||||
input := method.Input()
|
||||
out = append(out, &Tool{
|
||||
Name: toolName,
|
||||
Description: fmt.Sprintf("Call reflected gRPC method %s on %s", fullMethod, target.Address),
|
||||
InputSchema: protoMessageSchema(input),
|
||||
Handler: reflectedGRPCHandler(target, fullMethod, input, method.Output()),
|
||||
})
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func loadReflectedFiles(ctx context.Context, conn *grpc.ClientConn) (*protoregistryFiles, []string, error) {
|
||||
client := reflectionpb.NewServerReflectionClient(conn)
|
||||
stream, err := client.ServerReflectionInfo(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if err := stream.Send(&reflectionpb.ServerReflectionRequest{MessageRequest: &reflectionpb.ServerReflectionRequest_ListServices{ListServices: ""}}); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
resp, err := stream.Recv()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
list := resp.GetListServicesResponse()
|
||||
if list == nil {
|
||||
return nil, nil, fmt.Errorf("reflection list services returned %T", resp.MessageResponse)
|
||||
}
|
||||
|
||||
set := &descriptorpb.FileDescriptorSet{}
|
||||
seen := map[string]bool{}
|
||||
var services []string
|
||||
for _, svc := range list.Service {
|
||||
name := svc.Name
|
||||
if strings.HasPrefix(name, "grpc.reflection.") {
|
||||
continue
|
||||
}
|
||||
services = append(services, name)
|
||||
if err := requestFileContainingSymbol(ctx, client, name, set, seen); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
files, err := newProtoregistryFiles(set)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return files, services, nil
|
||||
}
|
||||
|
||||
func requestFileContainingSymbol(ctx context.Context, client reflectionpb.ServerReflectionClient, symbol string, set *descriptorpb.FileDescriptorSet, seen map[string]bool) error {
|
||||
stream, err := client.ServerReflectionInfo(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := stream.Send(&reflectionpb.ServerReflectionRequest{MessageRequest: &reflectionpb.ServerReflectionRequest_FileContainingSymbol{FileContainingSymbol: symbol}}); err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := stream.Recv()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fd := resp.GetFileDescriptorResponse()
|
||||
if fd == nil {
|
||||
return fmt.Errorf("reflection lookup for %s returned %T", symbol, resp.MessageResponse)
|
||||
}
|
||||
for _, raw := range fd.FileDescriptorProto {
|
||||
var file descriptorpb.FileDescriptorProto
|
||||
if err := proto.Unmarshal(raw, &file); err != nil {
|
||||
return err
|
||||
}
|
||||
name := file.GetName()
|
||||
if !seen[name] {
|
||||
seen[name] = true
|
||||
set.File = append(set.File, &file)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// protoregistryFiles is a narrow wrapper that keeps imports local to this file.
|
||||
type protoregistryFiles struct{ files *protoregistry.Files }
|
||||
|
||||
func newProtoregistryFiles(set *descriptorpb.FileDescriptorSet) (*protoregistryFiles, error) {
|
||||
files, err := protodesc.NewFiles(set)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &protoregistryFiles{files: files}, nil
|
||||
}
|
||||
|
||||
func (p *protoregistryFiles) FindDescriptorByName(name protoreflect.FullName) (protoreflect.Descriptor, error) {
|
||||
return p.files.FindDescriptorByName(name)
|
||||
}
|
||||
|
||||
func reflectedGRPCHandler(target ReflectedGRPCTarget, fullMethod string, input, output protoreflect.MessageDescriptor) func(map[string]interface{}) (interface{}, error) {
|
||||
return func(args map[string]interface{}) (interface{}, error) {
|
||||
timeout := target.Timeout
|
||||
if timeout == 0 {
|
||||
timeout = 10 * time.Second
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
dialOpts := target.DialOptions
|
||||
if len(dialOpts) == 0 {
|
||||
dialOpts = []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
|
||||
}
|
||||
conn, err := grpc.NewClient(target.Address, dialOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
req := dynamicpb.NewMessage(input)
|
||||
raw, err := json.Marshal(args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := protojson.Unmarshal(raw, req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rsp := dynamicpb.NewMessage(output)
|
||||
if err := conn.Invoke(ctx, fullMethod, req, rsp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b, err := protojson.MarshalOptions{UseProtoNames: true, EmitUnpopulated: true}.Marshal(rsp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var out interface{}
|
||||
if err := json.Unmarshal(b, &out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
}
|
||||
|
||||
func protoMessageSchema(msg protoreflect.MessageDescriptor) map[string]interface{} {
|
||||
schema := map[string]interface{}{"type": "object", "properties": map[string]interface{}{}}
|
||||
props := schema["properties"].(map[string]interface{})
|
||||
fields := msg.Fields()
|
||||
for i := 0; i < fields.Len(); i++ {
|
||||
field := fields.Get(i)
|
||||
props[field.JSONName()] = protoFieldSchema(field)
|
||||
}
|
||||
return schema
|
||||
}
|
||||
|
||||
func protoFieldSchema(field protoreflect.FieldDescriptor) map[string]interface{} {
|
||||
schema := map[string]interface{}{"type": protoJSONType(field)}
|
||||
if field.IsList() {
|
||||
schema["items"] = map[string]interface{}{"type": protoJSONType(field)}
|
||||
}
|
||||
if field.Kind() == protoreflect.MessageKind || field.Kind() == protoreflect.GroupKind {
|
||||
schema = protoMessageSchema(field.Message())
|
||||
}
|
||||
return schema
|
||||
}
|
||||
|
||||
func protoJSONType(field protoreflect.FieldDescriptor) string {
|
||||
if field.IsList() {
|
||||
return "array"
|
||||
}
|
||||
switch field.Kind() {
|
||||
case protoreflect.BoolKind:
|
||||
return "boolean"
|
||||
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind,
|
||||
protoreflect.Uint32Kind, protoreflect.Fixed32Kind, protoreflect.Int64Kind,
|
||||
protoreflect.Sint64Kind, protoreflect.Sfixed64Kind, protoreflect.Uint64Kind,
|
||||
protoreflect.Fixed64Kind:
|
||||
return "integer"
|
||||
case protoreflect.FloatKind, protoreflect.DoubleKind:
|
||||
return "number"
|
||||
case protoreflect.MessageKind, protoreflect.GroupKind:
|
||||
return "object"
|
||||
default:
|
||||
return "string"
|
||||
}
|
||||
}
|
||||
|
||||
func sanitizeToolPart(s string) string {
|
||||
r := strings.NewReplacer(":", "_", "/", "_", ".", "_", "-", "_")
|
||||
return r.Replace(s)
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package mcp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
helloworld "google.golang.org/grpc/examples/helloworld/helloworld"
|
||||
"google.golang.org/grpc/reflection"
|
||||
)
|
||||
|
||||
type reflectedGreeter struct {
|
||||
helloworld.UnimplementedGreeterServer
|
||||
}
|
||||
|
||||
func (reflectedGreeter) SayHello(_ context.Context, req *helloworld.HelloRequest) (*helloworld.HelloReply, error) {
|
||||
return &helloworld.HelloReply{Message: "hello " + req.Name}, nil
|
||||
}
|
||||
|
||||
func TestReflectedGRPCTargetDiscoversAndCallsUnaryTool(t *testing.T) {
|
||||
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
grpcServer := grpc.NewServer()
|
||||
helloworld.RegisterGreeterServer(grpcServer, reflectedGreeter{})
|
||||
reflection.Register(grpcServer)
|
||||
go grpcServer.Serve(lis)
|
||||
defer grpcServer.Stop()
|
||||
|
||||
s := newTestServer(Options{Context: context.Background()})
|
||||
tools, err := s.reflectedGRPCTools(ReflectedGRPCTarget{
|
||||
Name: "demo",
|
||||
Address: lis.Addr().String(),
|
||||
Timeout: 3 * time.Second,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("discover reflected tools: %v", err)
|
||||
}
|
||||
if len(tools) != 1 {
|
||||
t.Fatalf("tools len = %d, want 1", len(tools))
|
||||
}
|
||||
tool := tools[0]
|
||||
if tool.Name != "demo.helloworld_Greeter.SayHello" {
|
||||
t.Fatalf("tool name = %q", tool.Name)
|
||||
}
|
||||
props := tool.InputSchema["properties"].(map[string]interface{})
|
||||
if _, ok := props["name"]; !ok {
|
||||
t.Fatalf("input schema missing name: %#v", tool.InputSchema)
|
||||
}
|
||||
|
||||
out, err := tool.Handler(map[string]interface{}{"name": "Ada"})
|
||||
if err != nil {
|
||||
t.Fatalf("call reflected tool: %v", err)
|
||||
}
|
||||
got := out.(map[string]interface{})["message"]
|
||||
if got != "hello Ada" {
|
||||
t.Fatalf("message = %v, want hello Ada", got)
|
||||
}
|
||||
}
|
||||
@@ -157,6 +157,11 @@ type Options struct {
|
||||
// (the /mcp/call endpoint). Listing tools and health stay free.
|
||||
// Opt-in: leave nil to disable payments.
|
||||
Payment *x402.Config
|
||||
|
||||
// ReflectedGRPCTargets exposes unary methods from external gRPC servers
|
||||
// that support server reflection as MCP tools. This bridges existing gRPC
|
||||
// services into the agent tool catalog without requiring go-micro handlers.
|
||||
ReflectedGRPCTargets []ReflectedGRPCTarget
|
||||
}
|
||||
|
||||
// Server represents a running MCP gateway
|
||||
@@ -286,6 +291,10 @@ func (s *Server) discoverServices() error {
|
||||
s.toolsMu.Lock()
|
||||
defer s.toolsMu.Unlock()
|
||||
|
||||
if err := s.discoverReflectedGRPC(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, svc := range services {
|
||||
// Get full service details
|
||||
fullSvcs, err := s.opts.Registry.GetService(svc.Name)
|
||||
|
||||
+5
-19
@@ -294,7 +294,9 @@ func (t *StdioTransport) handleToolsCall(req *JSONRPCRequest) {
|
||||
AccountID: accountID, ScopesRequired: tool.Scopes,
|
||||
Allowed: true, Duration: time.Since(start), Error: err.Error(),
|
||||
})
|
||||
t.sendError(req.ID, InternalError, "RPC call failed", err.Error())
|
||||
// A tool-execution failure is reported as an isError result, not a
|
||||
// JSON-RPC protocol error (per the MCP spec), so the agent can read it.
|
||||
t.sendResponse(req.ID, mcpToolError(traceID, "tool call failed: "+err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -311,24 +313,8 @@ func (t *StdioTransport) handleToolsCall(req *JSONRPCRequest) {
|
||||
Allowed: true, Duration: time.Since(start),
|
||||
})
|
||||
|
||||
// Parse response
|
||||
var result interface{}
|
||||
if err := json.Unmarshal(rsp.Data, &result); err != nil {
|
||||
// If unmarshal fails, return raw data
|
||||
result = map[string]interface{}{
|
||||
"data": string(rsp.Data),
|
||||
}
|
||||
}
|
||||
|
||||
t.sendResponse(req.ID, map[string]interface{}{
|
||||
"content": []interface{}{
|
||||
map[string]interface{}{
|
||||
"type": "text",
|
||||
"text": fmt.Sprintf("%v", result),
|
||||
},
|
||||
},
|
||||
"trace_id": traceID,
|
||||
})
|
||||
// The downstream response is JSON — return it as JSON text, not %v.
|
||||
t.sendResponse(req.ID, mcpToolResult(traceID, rsp.Data))
|
||||
}
|
||||
|
||||
// sendResponse sends a JSON-RPC response
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
package mcp
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"go-micro.dev/v6/client"
|
||||
)
|
||||
|
||||
// fakeCallClient overrides Call to return canned data or an error; NewRequest
|
||||
// and the rest are promoted from the embedded real client.
|
||||
type fakeCallClient struct {
|
||||
client.Client
|
||||
data []byte
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *fakeCallClient) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
||||
if f.err != nil {
|
||||
return f.err
|
||||
}
|
||||
if r, ok := rsp.(*struct{ Data []byte }); ok {
|
||||
r.Data = f.data
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// isToolError reports whether an MCP tools/call result carries isError:true.
|
||||
func isToolError(result interface{}) bool {
|
||||
m, ok := result.(map[string]interface{})
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
b, _ := m["isError"].(bool)
|
||||
return b
|
||||
}
|
||||
|
||||
// toolResultText extracts the first text content of an MCP tools/call result.
|
||||
func toolResultText(t *testing.T, result interface{}) string {
|
||||
t.Helper()
|
||||
m, ok := result.(map[string]interface{})
|
||||
if !ok {
|
||||
t.Fatalf("result is not a map: %#v", result)
|
||||
}
|
||||
content, ok := m["content"].([]interface{})
|
||||
if !ok || len(content) == 0 {
|
||||
t.Fatalf("result has no content: %#v", result)
|
||||
}
|
||||
first, _ := content[0].(map[string]interface{})
|
||||
text, _ := first["text"].(string)
|
||||
return text
|
||||
}
|
||||
|
||||
// driveStdio sends one JSON-RPC request through a StdioTransport and returns the
|
||||
// decoded response, capturing the transport's stdout into a buffer.
|
||||
func driveStdio(t *testing.T, s *Server, method string, id interface{}, params interface{}) JSONRPCResponse {
|
||||
t.Helper()
|
||||
tr := NewStdioTransport(s)
|
||||
var out bytes.Buffer
|
||||
tr.writer = bufio.NewWriter(&out)
|
||||
raw, _ := json.Marshal(params)
|
||||
tr.handleRequest(&JSONRPCRequest{JSONRPC: "2.0", ID: id, Method: method, Params: raw})
|
||||
var resp JSONRPCResponse
|
||||
if err := json.Unmarshal(bytes.TrimSpace(out.Bytes()), &resp); err != nil {
|
||||
t.Fatalf("decode stdio response: %v (raw=%q)", err, out.String())
|
||||
}
|
||||
return resp
|
||||
}
|
||||
|
||||
// The stdio transport is the path an external MCP host (Claude Desktop) uses.
|
||||
// It must return tool output as JSON text, not fmt.Sprintf("%v", ...) which
|
||||
// yields Go map-syntax and is unparseable by a real client.
|
||||
func TestStdio_ToolsCall_ReturnsJSONNotGoSyntax(t *testing.T) {
|
||||
s := newTestServer(Options{})
|
||||
s.opts.Client = &fakeCallClient{Client: client.DefaultClient, data: []byte(`{"id":1,"name":"bob"}`)}
|
||||
s.tools["svc.Echo"] = &Tool{Name: "svc.Echo", Service: "svc", Endpoint: "Echo"}
|
||||
|
||||
resp := driveStdio(t, s, "tools/call", 1, map[string]interface{}{
|
||||
"name": "svc.Echo",
|
||||
"arguments": map[string]interface{}{"msg": "hi"},
|
||||
})
|
||||
if resp.Error != nil {
|
||||
t.Fatalf("unexpected protocol error: %+v", resp.Error)
|
||||
}
|
||||
text := toolResultText(t, resp.Result)
|
||||
// The bug returned Go map-syntax ("map[id:1 name:bob]"), which fails to parse.
|
||||
var got map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(text), &got); err != nil {
|
||||
t.Fatalf("tool result text is not JSON (the %%v bug): %q", text)
|
||||
}
|
||||
if got["name"] != "bob" {
|
||||
t.Errorf("result = %v, want name=bob", got)
|
||||
}
|
||||
}
|
||||
|
||||
// A tool-execution failure must be an MCP isError result, not a JSON-RPC
|
||||
// protocol error, so the agent can read the failure.
|
||||
func TestStdio_ToolsCall_FailureIsIsErrorResult(t *testing.T) {
|
||||
s := newTestServer(Options{})
|
||||
s.opts.Client = &fakeCallClient{Client: client.DefaultClient, err: errors.New("backend down")}
|
||||
s.tools["svc.Echo"] = &Tool{Name: "svc.Echo", Service: "svc", Endpoint: "Echo"}
|
||||
|
||||
resp := driveStdio(t, s, "tools/call", 1, map[string]interface{}{
|
||||
"name": "svc.Echo",
|
||||
"arguments": map[string]interface{}{},
|
||||
})
|
||||
if resp.Error != nil {
|
||||
t.Fatalf("tool failure returned a protocol error, want isError result: %+v", resp.Error)
|
||||
}
|
||||
if !isToolError(resp.Result) {
|
||||
t.Fatalf("expected isError result, got %+v", resp.Result)
|
||||
}
|
||||
if text := toolResultText(t, resp.Result); text == "" {
|
||||
t.Error("isError result should carry the error text")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package mcp
|
||||
|
||||
// MCP tools/call result shaping, shared by the stdio and websocket JSON-RPC
|
||||
// transports. Kept in one place so both transports produce spec-shaped results.
|
||||
|
||||
// mcpToolResult builds a successful MCP tools/call result. The downstream RPC
|
||||
// response body (data) is JSON, so it is returned as JSON text — NOT
|
||||
// fmt.Sprintf("%v", ...) of a decoded value, which produces Go map-syntax
|
||||
// (map[id:1 name:bob]) instead of JSON and is what an external MCP client
|
||||
// (e.g. Claude Desktop over stdio) would otherwise receive.
|
||||
func mcpToolResult(traceID string, data []byte) map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"content": []interface{}{
|
||||
map[string]interface{}{"type": "text", "text": string(data)},
|
||||
},
|
||||
"trace_id": traceID,
|
||||
}
|
||||
}
|
||||
|
||||
// mcpToolError builds an MCP tools/call result for a tool-EXECUTION failure.
|
||||
// Per the MCP spec a tool that fails returns a normal result with isError:true
|
||||
// (the error as text content), NOT a JSON-RPC protocol error — that way the
|
||||
// agent can read the failure instead of seeing a transport-level error.
|
||||
func mcpToolError(traceID, msg string) map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"content": []interface{}{
|
||||
map[string]interface{}{"type": "text", "text": msg},
|
||||
},
|
||||
"isError": true,
|
||||
"trace_id": traceID,
|
||||
}
|
||||
}
|
||||
@@ -275,7 +275,8 @@ func (wc *wsConn) handleToolsCall(req *JSONRPCRequest) {
|
||||
AccountID: accountID, ScopesRequired: tool.Scopes,
|
||||
Allowed: true, Duration: time.Since(start), Error: err.Error(),
|
||||
})
|
||||
wc.sendError(req.ID, InternalError, "RPC call failed", err.Error())
|
||||
// Tool-execution failure → isError result (MCP spec), not a protocol error.
|
||||
wc.sendResponse(req.ID, mcpToolError(traceID, "tool call failed: "+err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -291,23 +292,8 @@ func (wc *wsConn) handleToolsCall(req *JSONRPCRequest) {
|
||||
Allowed: true, Duration: time.Since(start),
|
||||
})
|
||||
|
||||
// Parse response
|
||||
var result interface{}
|
||||
if err := json.Unmarshal(rsp.Data, &result); err != nil {
|
||||
result = map[string]interface{}{
|
||||
"data": string(rsp.Data),
|
||||
}
|
||||
}
|
||||
|
||||
wc.sendResponse(req.ID, map[string]interface{}{
|
||||
"content": []interface{}{
|
||||
map[string]interface{}{
|
||||
"type": "text",
|
||||
"text": fmt.Sprintf("%v", result),
|
||||
},
|
||||
},
|
||||
"trace_id": traceID,
|
||||
})
|
||||
// The downstream response is JSON — return it as JSON text, not %v.
|
||||
wc.sendResponse(req.ID, mcpToolResult(traceID, rsp.Data))
|
||||
}
|
||||
|
||||
// sendResponse sends a JSON-RPC success response.
|
||||
|
||||
@@ -114,12 +114,13 @@ func TestWebSocket_ToolsCall_NoAuth(t *testing.T) {
|
||||
"arguments": map[string]interface{}{"msg": "hi"},
|
||||
})
|
||||
|
||||
// RPC will fail (no backend), but auth should pass (no auth configured)
|
||||
if resp.Error == nil {
|
||||
t.Fatal("expected RPC error (no backend)")
|
||||
// No auth required → the tool runs; the RPC fails (no backend), which the
|
||||
// MCP spec surfaces as an isError result, not a JSON-RPC protocol error.
|
||||
if resp.Error != nil {
|
||||
t.Fatalf("expected no protocol error, got %+v", resp.Error)
|
||||
}
|
||||
if resp.Error.Code != InternalError {
|
||||
t.Errorf("error code = %d, want %d", resp.Error.Code, InternalError)
|
||||
if !isToolError(resp.Result) {
|
||||
t.Fatalf("expected isError tool result, got %+v", resp.Result)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,12 +169,13 @@ func TestWebSocket_ToolsCall_AuthRequired(t *testing.T) {
|
||||
"arguments": map[string]interface{}{},
|
||||
"_token": "valid-token",
|
||||
})
|
||||
// Auth passes, RPC fails (no backend)
|
||||
if resp.Error == nil {
|
||||
t.Fatal("expected RPC error")
|
||||
// Auth passes → the tool runs; RPC fails (no backend) → isError result,
|
||||
// not a JSON-RPC protocol error (which would mean auth failed).
|
||||
if resp.Error != nil {
|
||||
t.Fatalf("expected no protocol error (auth passed), got %+v", resp.Error)
|
||||
}
|
||||
if resp.Error.Code != InternalError {
|
||||
t.Errorf("error code = %d, want %d (RPC fail, not auth fail)", resp.Error.Code, InternalError)
|
||||
if !isToolError(resp.Result) {
|
||||
t.Fatalf("expected isError tool result, got %+v", resp.Result)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -185,12 +187,13 @@ func TestWebSocket_ToolsCall_AuthRequired(t *testing.T) {
|
||||
"name": "svc.Do",
|
||||
"arguments": map[string]interface{}{},
|
||||
})
|
||||
// Auth passes via connection-level header, RPC fails (no backend)
|
||||
if resp.Error == nil {
|
||||
t.Fatal("expected RPC error")
|
||||
// Auth passes via connection-level header → tool runs; RPC fails (no
|
||||
// backend) → isError result, not a JSON-RPC protocol error.
|
||||
if resp.Error != nil {
|
||||
t.Fatalf("expected no protocol error (auth passed), got %+v", resp.Error)
|
||||
}
|
||||
if resp.Error.Code != InternalError {
|
||||
t.Errorf("error code = %d, want %d (RPC fail, not auth fail)", resp.Error.Code, InternalError)
|
||||
if !isToolError(resp.Result) {
|
||||
t.Fatalf("expected isError tool result, got %+v", resp.Result)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -166,7 +166,9 @@ func main() {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if summary.WorkingEvents == 0 || summary.State != "completed" || !strings.Contains(summary.FinalText, "a2a-stream-ok") {
|
||||
// Spec-shaped stream: at least one artifact-update carrying the reassembled
|
||||
// answer, terminating in a completed status-update with final:true.
|
||||
if summary.ArtifactEvents == 0 || summary.State != "completed" || !summary.Final || !strings.Contains(summary.FinalText, "a2a-stream-ok") {
|
||||
fmt.Fprintf(os.Stderr, "unexpected stream summary: %+v\npayload:\n%s", summary, summary.Payload)
|
||||
os.Exit(1)
|
||||
}
|
||||
@@ -174,14 +176,16 @@ func main() {
|
||||
fmt.Fprintf(os.Stderr, "tool=%v runInfo=%v\n", sawTool, sawRunInfo)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println("\n\033[32m✓ A2A message/stream emitted incremental task updates and preserved tool/run metadata\033[0m")
|
||||
fmt.Println("\n\033[32m✓ A2A message/stream emitted spec-shaped artifact/status updates and preserved tool/run metadata\033[0m")
|
||||
}
|
||||
|
||||
type streamSummary struct {
|
||||
Payload string
|
||||
State string
|
||||
FinalText string
|
||||
WorkingEvents int
|
||||
Payload string
|
||||
State string
|
||||
FinalText string
|
||||
Final bool
|
||||
ArtifactEvents int
|
||||
WorkingEvents int
|
||||
}
|
||||
|
||||
func readSSESummary(r io.Reader) (streamSummary, error) {
|
||||
@@ -197,14 +201,23 @@ func readSSESummary(r io.Reader) (streamSummary, error) {
|
||||
}
|
||||
var envelope struct {
|
||||
Result struct {
|
||||
Kind string `json:"kind"`
|
||||
Final bool `json:"final"`
|
||||
Status struct {
|
||||
State string `json:"state"`
|
||||
} `json:"status"`
|
||||
// Task snapshots carry artifacts (plural)...
|
||||
Artifacts []struct {
|
||||
Parts []struct {
|
||||
Text string `json:"text"`
|
||||
} `json:"parts"`
|
||||
} `json:"artifacts"`
|
||||
// ...artifact-update events carry a single artifact.
|
||||
Artifact struct {
|
||||
Parts []struct {
|
||||
Text string `json:"text"`
|
||||
} `json:"parts"`
|
||||
} `json:"artifact"`
|
||||
} `json:"result"`
|
||||
Error any `json:"error"`
|
||||
}
|
||||
@@ -216,15 +229,34 @@ func readSSESummary(r io.Reader) (streamSummary, error) {
|
||||
}
|
||||
seen = true
|
||||
summary.Payload += data + "\n"
|
||||
if envelope.Result.Status.State == "working" {
|
||||
summary.WorkingEvents++
|
||||
}
|
||||
if envelope.Result.Status.State != "" {
|
||||
summary.State = envelope.Result.Status.State
|
||||
}
|
||||
for _, artifact := range envelope.Result.Artifacts {
|
||||
for _, part := range artifact.Parts {
|
||||
summary.FinalText = part.Text
|
||||
switch envelope.Result.Kind {
|
||||
case "artifact-update":
|
||||
// Incremental deltas: reassemble the streamed answer.
|
||||
summary.ArtifactEvents++
|
||||
for _, part := range envelope.Result.Artifact.Parts {
|
||||
summary.FinalText += part.Text
|
||||
}
|
||||
case "status-update":
|
||||
if envelope.Result.Status.State != "" {
|
||||
summary.State = envelope.Result.Status.State
|
||||
}
|
||||
if envelope.Result.Final {
|
||||
summary.Final = true
|
||||
}
|
||||
default: // "task" snapshot
|
||||
if envelope.Result.Status.State == "working" {
|
||||
summary.WorkingEvents++
|
||||
}
|
||||
if envelope.Result.Status.State != "" {
|
||||
summary.State = envelope.Result.Status.State
|
||||
}
|
||||
// The non-streaming path carries the full text in the snapshot.
|
||||
for _, artifact := range envelope.Result.Artifacts {
|
||||
for _, part := range artifact.Parts {
|
||||
if part.Text != "" {
|
||||
summary.FinalText = part.Text
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
+11
-1
@@ -8,6 +8,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -81,7 +82,16 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
||||
return resp, fmt.Errorf("x402: 402 response carried no requirements")
|
||||
}
|
||||
reqd := ch.Accepts[0]
|
||||
amount, _ := strconv.ParseInt(reqd.MaxAmountRequired, 10, 64)
|
||||
// The amount governs the whole spend cap, so it must be a real positive
|
||||
// integer. A swallowed parse error (non-decimal, overflow, empty) would
|
||||
// yield 0 and pass the budget check trivially, and a negative amount would
|
||||
// inflate the remaining allowance — either way the cap is defeated. Refuse
|
||||
// before signing anything.
|
||||
amount, err := strconv.ParseInt(strings.TrimSpace(reqd.MaxAmountRequired), 10, 64)
|
||||
if err != nil || amount <= 0 {
|
||||
return resp, fmt.Errorf("x402: refusing to pay %s: invalid maxAmountRequired %q",
|
||||
reqd.Resource, reqd.MaxAmountRequired)
|
||||
}
|
||||
|
||||
// Spend cap: reserve before paying so concurrent calls cannot all pass
|
||||
// the check and overspend the caller's allowance. Roll the reservation
|
||||
|
||||
@@ -172,6 +172,33 @@ func TestClientBudgetReservationRollsBackOnPayError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A 402 whose maxAmountRequired is not a positive integer must be refused
|
||||
// before any payment — otherwise a swallowed parse error (0) or a negative
|
||||
// amount defeats the spend cap. The payer is never called and nothing is spent.
|
||||
func TestClientRefusesInvalidAmount(t *testing.T) {
|
||||
for _, amount := range []string{"abc", "-100", "99999999999999999999999999", "0x10", "1.5"} {
|
||||
srv := paidServer(amount)
|
||||
|
||||
payer := &mockPayer{}
|
||||
c := &Client{Payer: payer, Budget: 1_000_000}
|
||||
req, _ := http.NewRequest(http.MethodGet, srv.URL, nil)
|
||||
resp, err := c.Do(req)
|
||||
if resp != nil {
|
||||
resp.Body.Close()
|
||||
}
|
||||
if err == nil {
|
||||
t.Errorf("amount %q: expected refusal, got nil error", amount)
|
||||
}
|
||||
if payer.calls != 0 {
|
||||
t.Errorf("amount %q: payer called %d times, want 0", amount, payer.calls)
|
||||
}
|
||||
if c.Spent() != 0 {
|
||||
t.Errorf("amount %q: spent %d, want 0", amount, c.Spent())
|
||||
}
|
||||
srv.Close()
|
||||
}
|
||||
}
|
||||
|
||||
type payerFunc func(context.Context, Requirements) (string, error)
|
||||
|
||||
func (f payerFunc) Pay(ctx context.Context, req Requirements) (string, error) {
|
||||
|
||||
+13
-1
@@ -126,6 +126,11 @@ type Config struct {
|
||||
// FacilitatorURL is the verify/settle endpoint used when Facilitator
|
||||
// is nil (e.g. Coinbase CDP or Alchemy).
|
||||
FacilitatorURL string `json:"facilitator,omitempty"`
|
||||
// RequireSettlement fails closed when a paid request cannot be settled:
|
||||
// if the facilitator only verifies (does not implement Settler), Require
|
||||
// refuses to serve rather than releasing the resource while no funds move.
|
||||
// Leave false only for verify-only flows where authorization is enough.
|
||||
RequireSettlement bool `json:"requireSettlement,omitempty"`
|
||||
}
|
||||
|
||||
func (c Config) network() string {
|
||||
@@ -222,7 +227,14 @@ func (c Config) Require(w http.ResponseWriter, r *http.Request, amount, resource
|
||||
}
|
||||
// Capture the funds when the facilitator can settle. Verify alone only
|
||||
// authorizes the "exact" transfer; settlement broadcasts it.
|
||||
if s, ok := fac.(Settler); ok {
|
||||
s, canSettle := fac.(Settler)
|
||||
if c.RequireSettlement && !canSettle {
|
||||
// Fail closed: a paid config must not serve the resource on a
|
||||
// verify-only facilitator, or it gives the tool away for free.
|
||||
writeChallenge(w, req, "payment settlement unavailable")
|
||||
return false
|
||||
}
|
||||
if canSettle {
|
||||
sres, err := s.Settle(r.Context(), payment, req)
|
||||
if err != nil {
|
||||
writeChallenge(w, req, "payment settlement failed: "+err.Error())
|
||||
|
||||
@@ -159,4 +159,58 @@ func TestCDPAuthorizeAttachesBearer(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestRequireSettlementFailsClosed checks that a paid config with
|
||||
// RequireSettlement refuses to serve when the facilitator only verifies (does
|
||||
// not settle) — otherwise the resource is released while no funds move.
|
||||
func TestRequireSettlementFailsClosed(t *testing.T) {
|
||||
// mockFacilitator implements Verify but not Settler.
|
||||
cfg := Config{PayTo: "0xpay", Facilitator: mockFacilitator{valid: true}, RequireSettlement: true}
|
||||
r := httptest.NewRequest(http.MethodGet, "/tool", nil)
|
||||
r.Header.Set(PaymentHeader, "eyJ4IjoxfQ==")
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
if cfg.Require(rec, r, "10000", "chat") {
|
||||
t.Fatal("Require should fail closed when settlement is required but unavailable")
|
||||
}
|
||||
if rec.Code != http.StatusPaymentRequired {
|
||||
t.Errorf("status = %d, want 402", rec.Code)
|
||||
}
|
||||
|
||||
// Without RequireSettlement the verify-only facilitator still serves.
|
||||
cfg.RequireSettlement = false
|
||||
rec = httptest.NewRecorder()
|
||||
r = httptest.NewRequest(http.MethodGet, "/tool", nil)
|
||||
r.Header.Set(PaymentHeader, "eyJ4IjoxfQ==")
|
||||
if !cfg.Require(rec, r, "10000", "chat") {
|
||||
t.Fatalf("verify-only should serve when settlement is not required; body=%s", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestRequireSettlementServesWithSettler checks that a paid config with
|
||||
// RequireSettlement serves when the facilitator can settle.
|
||||
func TestRequireSettlementServesWithSettler(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/verify":
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"isValid": true})
|
||||
case "/settle":
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"success": true, "transaction": "0xabc"})
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
// HTTPFacilitator implements Settler.
|
||||
cfg := Config{PayTo: "0xpay", FacilitatorURL: srv.URL, RequireSettlement: true}
|
||||
r := httptest.NewRequest(http.MethodGet, "/tool", nil)
|
||||
r.Header.Set(PaymentHeader, "eyJ4IjoxfQ==")
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
if !cfg.Require(rec, r, "10000", "chat") {
|
||||
t.Fatalf("Require should serve with a settling facilitator; body=%s", rec.Body.String())
|
||||
}
|
||||
if got := rec.Header().Get(PaymentResponseHeader); got != "0xabc" {
|
||||
t.Errorf("settlement header = %q, want 0xabc", got)
|
||||
}
|
||||
}
|
||||
|
||||
var _ Settler = (*HTTPFacilitator)(nil)
|
||||
|
||||
Reference in New Issue
Block a user