a2x-integration
Integrates the @a2x/sdk (A2A Agent-to-Agent protocol) into TypeScript projects, including paid merchant agents using x402. Supports Next.js App Router, Next.js Pages Router, Express, NestJS, and zero-framework quick start. Use when the user wants to add A2A agent capabilities, an a2x endpoint, or MerchantGate-based payments using @a2x/sdk.
- License 许可
- MIT
- 所属仓库
- planetarium/a2x
- 最近更新
- 2026-11-06
- Stars
- ★ 2
- Forks
- 3
01 前言 Front Matter
- name
- a2x-integration
- description
- Integrates the @a2x/sdk (A2A Agent-to-Agent protocol) into TypeScript projects, including paid merchant agents using x402. Supports Next.js App Router, Next.js Pages Router, Express, NestJS, and zero-framework quick start. Use when the user wants to add A2A agent capabilities, an a2x endpoint, or MerchantGate-based payments using @a2x/sdk.
02 资源文件 11
- 参考文档wiki/concepts.md6.0 KB
- 参考文档wiki/express.md4.9 KB
- 参考文档wiki/nestjs.md6.3 KB
- 参考文档wiki/nextjs-app-router.md5.2 KB
- 参考文档wiki/nextjs-pages-router.md4.4 KB
- 参考文档wiki/providers.md1.9 KB
- 参考文档wiki/quick-start.md3.5 KB
- 参考文档wiki/security.md4.5 KB
- 参考文档wiki/streaming.md4.2 KB
- 参考文档wiki/tools-and-agents.md3.1 KB
- 参考文档wiki/x402-payments.md6.9 KB
03 SKILL.md 正文
a2x-integration
Add A2A (Agent-to-Agent) protocol support to any TypeScript project using @a2x/sdk.
@a2x/sdk is a self-contained A2A SDK with zero runtime dependencies (only Node.js built-ins). It includes an agent runtime, LLM providers, authentication, task management, and HTTP transport — all in one package.
Before You Start
IMPORTANT: The @a2x/sdk package evolves quickly. Before writing any code:
- Install the package first (see Step 1).
- Read actual type definitions from
node_modules/@a2x/sdkto confirm current API signatures. - If a class, function, or import path referenced in this skill does not exist in the installed version, search
node_modules/@a2x/sdkfor the closest equivalent. - Never hardcode package versions. Always install the latest.
Wiki Reference
This skill uses a wiki-style structure. Detailed reference material is in the wiki/ directory:
| Topic | File | Description |
|---|---|---|
| Core Concepts | wiki/concepts.md | Architecture layers, key classes, protocol versions |
| LLM Providers | wiki/providers.md | Google, Anthropic, OpenAI provider setup |
| Tools & Agents | wiki/tools-and-agents.md | FunctionTool, AgentTool, multi-agent patterns |
| Security | wiki/security.md | API Key, Bearer, OAuth2, security requirements |
| SSE Streaming | wiki/streaming.md | createSSEStream, AsyncGenerator, SSE headers |
| Merchant x402 Payments | wiki/x402-payments.md | MerchantGate policy decisions, delivery timing, settlement, and durable stores |
| Quick Start | wiki/quick-start.md | toA2x() helper for zero-framework prototyping |
Framework Guides
| Framework | File |
|---|---|
| Next.js App Router | wiki/nextjs-app-router.md |
| Next.js Pages Router | wiki/nextjs-pages-router.md |
| Express | wiki/express.md |
| NestJS | wiki/nestjs.md |
Workflow
Step 0 — Analyze the Existing Project
Before any implementation:
- Read
package.jsonto identify the HTTP framework, package manager, and existing dependencies. - Inspect
src/(or project root) to understand the directory layout and conventions. - Determine the framework type:
- Next.js App Router —
src/app/orapp/directory withroute.tsfiles - Next.js Pages Router —
src/pages/orpages/directory withapi/folder - Express —
expressin dependencies, typicallyapp.tsorserver.ts - NestJS —
@nestjs/corein dependencies, module/controller/service pattern - None / Quick prototype — No HTTP framework, or user wants minimal setup
- Next.js App Router —
- Check for an existing
.env.exampleor.envfile to understand environment variable conventions. - Identify which LLM provider the user wants (Google Gemini, Anthropic Claude, OpenAI GPT). If unclear, ask the user.
- If the agent charges through x402 or streams paid output, read wiki/x402-payments.md before implementing its execution lifecycle.
Step 1 — Install Packages
# Use the project's package manager (npm, pnpm, yarn, bun)
npm install @a2x/sdk
Install the LLM provider SDK as a peer dependency based on user choice:
# For Google Gemini
npm install @google/genai
# For Anthropic Claude
npm install @anthropic-ai/sdk
# For OpenAI GPT
npm install openai
After installation, verify key exports exist:
grep -r "export" node_modules/@a2x/sdk/dist/index.d.ts 2>/dev/null | head -30
Look for these key exports:
| Export | Purpose |
|---|---|
LlmAgent |
Agent with LLM provider and tools |
InMemoryRunner |
Agent execution runtime |
AgentExecutor |
Bridges runner with A2A task lifecycle |
InMemoryTaskStore |
In-memory task storage |
A2XServer |
A2A protocol integration (AgentCard, skills, security) |
DefaultRequestHandler |
Framework-agnostic JSON-RPC handler |
createSSEStream |
SSE streaming helper |
FunctionTool |
Wrap async functions as tools |
Step 2 — Set Up Environment Variables
Create or update .env.example:
# LLM Provider API Key (choose one based on your provider)
GOOGLE_API_KEY=
# ANTHROPIC_API_KEY=
# OPENAI_API_KEY=
# Public base URL of this server (used in AgentCard)
BASE_URL=http://localhost:3000
# Optional: Security
# API_KEYS=key1,key2,key3
Ensure .env.local (or .env) is in .gitignore.
Step 3 — Create the A2X Setup Module
This is the core integration. Create an a2x setup file (e.g., lib/a2x-setup.ts, src/lib/a2x-setup.ts, or wherever the project keeps shared modules).
Read wiki/concepts.md for the full architecture, then create the setup:
import {
LlmAgent,
InMemoryRunner,
AgentExecutor,
StreamingMode,
InMemoryTaskStore,
A2XServer,
DefaultRequestHandler,
} from '@a2x/sdk';
// Choose your provider — see wiki/providers.md
import { GoogleProvider } from '@a2x/sdk/google';
// import { AnthropicProvider } from '@a2x/sdk/anthropic';
// import { OpenAIProvider } from '@a2x/sdk/openai';
// 1. Define the LLM agent
const agent = new LlmAgent({
name: 'my-agent',
description: 'A helpful AI agent.',
provider: new GoogleProvider({
model: 'gemini-2.5-flash',
apiKey: process.env.GOOGLE_API_KEY!,
}),
instruction: 'You are a helpful assistant. Answer clearly and concisely.',
// tools: [], // Add FunctionTools here — see wiki/tools-and-agents.md
});
// 2. Create the runtime
const runner = new InMemoryRunner({ agent, appName: agent.name });
const executor = new AgentExecutor({
runner,
runConfig: { streamingMode: StreamingMode.SSE },
});
const taskStore = new InMemoryTaskStore();
// 3. Create the A2XServer (A2A protocol bridge)
export const a2xServer = new A2XServer({ taskStore, executor, protocolVersion: '1.0' })
.setDefaultUrl(`${process.env.BASE_URL ?? 'http://localhost:3000'}/a2a`)
.addSkill({
id: 'chat',
name: 'General Chat',
description: 'General-purpose conversation and Q&A',
tags: ['chat', 'general'],
});
// Add security — see wiki/security.md
// .addSecurityScheme('apiKey', new ApiKeyAuthorization({ ... }))
// .addSecurityRequirement({ apiKey: [] })
// 4. Create the request handler
export const handler = new DefaultRequestHandler(a2xServer);
Customization points for the user:
name/description— agent identityprovider— which LLM to use (see wiki/providers.md)instruction— system prompt that defines agent behaviortools— array of tools (see wiki/tools-and-agents.md)skills— A2A skills advertised in the AgentCard- Security — authentication schemes (see wiki/security.md)
- Payments — merchant pricing and delivery policy (see wiki/x402-payments.md)
Step 4 — Implement Route Handlers
Choose the framework guide matching the project:
- Next.js App Router → wiki/nextjs-app-router.md
- Next.js Pages Router → wiki/nextjs-pages-router.md
- Express → wiki/express.md
- NestJS → wiki/nestjs.md
- Quick prototype (no framework) → wiki/quick-start.md
Each guide provides:
- Agent Card endpoint (
GET /.well-known/agent.json) - JSON-RPC endpoint (
POST /a2a) - SSE streaming setup
- Framework-specific notes
The core logic is framework-agnostic (see wiki/streaming.md):
const result = await handler.handle(body, context);
if (result && typeof result === 'object' && Symbol.asyncIterator in result) {
// Streaming: AsyncGenerator → convert to SSE response
} else {
// Synchronous: plain object → return as JSON
}
Step 5 — Verify
- Build check: Run the project's build command to ensure no type errors.
- Agent Card:
curl http://localhost:<port>/.well-known/agent.json— should return valid JSON withname,url,skills,capabilities. - Send message:
curl -X POST http://localhost:<port>/a2a \ -H "Content-Type: application/json" \ -d '{"jsonrpc":"2.0","id":1,"method":"message/send","params":{"message":{"role":"user","parts":[{"kind":"text","text":"Hello"}]}}}' - Remind the user to set their LLM provider API key if not already done.
- Optionally test with
a2xCLI:a2x a2a agent-card http://localhost:<port> a2x a2a send http://localhost:<port> "Hello, agent!"
Exposed Endpoints
| Endpoint | Method | Description |
|---|---|---|
/.well-known/agent.json |
GET | AgentCard discovery (A2A standard) |
/a2a |
POST | JSON-RPC 2.0 endpoint for A2A messages |
The paths can be customized — update setDefaultUrl() on the A2XServer to match.
Key Differences from ts-a2a-integration Skill
This skill uses @a2x/sdk instead of @a2a-js/sdk + @google/adk. Key differences:
| Feature | @a2x/sdk |
@a2a-js/sdk + @google/adk |
|---|---|---|
| Dependencies | Single package | Two packages |
| LLM Providers | Google, Anthropic, OpenAI built-in | Google only (via ADK) |
| Protocol Versions | v0.3 + v1.0 from single definition | v1.0 only |
| AgentCard | Auto-generated via A2XServer builder |
Manual JSON object |
| Authentication | Built-in security scheme classes | Manual implementation |
| Handler | DefaultRequestHandler (framework-agnostic) |
JsonRpcTransportHandler |
| Quick Start | toA2x() helper with built-in HTTP server |
Not available |
After Applying
Remind the user to:
- Set their LLM provider API key in the environment
- Customize the agent: name, description, instruction (system prompt), and skills
- Add tools if needed (see wiki/tools-and-agents.md)
- Configure security if needed (see wiki/security.md)
- Test with
curlor thea2xCLI
04 同仓库更多 Skills
a2x-client-integration
Integrates `@a2x/sdk` **client-side** into a TypeScript application — call remote A2A agents from a Node.js CLI, Express/Fastify backend, Next.js server action, or background worker. Covers `A2XClient`, the `AuthProvider` fallback chain, `AuthScheme` handling, token persistence, SSE streaming consumption, and error mapping. Use when the user says things like "call an A2A agent", "use A2XClient", "consume an a2x agent", "add a2x client", "authenticate against an a2x agent", "stream from an a2x agent", or wants to embed remote agent access into their own app.
weather-report
Produce a compact weather report for a specified city. Use when the user asks about the weather, forecast, or temperature for a named city.
recipe-suggest
Suggest recipes based on a small list of ingredients the user already has. Use when the user lists ingredients and asks what they can cook, or mentions pantry, fridge, or leftover ingredients.
valid-simple
A minimal skill used by unit tests. Used when parser/loader coverage requires the smallest possible valid SKILL.md.
valid-full
A skill exercising every frontmatter feature the SDK supports.
invalid-no-frontmatter
暂无简介