swap-mcp
OAuth-protected Uniswap swap service that enables AI agents to get price quotes and execute ETH↔USDC swaps via MCP tools or REST API.
README
swap-mcp
OAuth-protected Uniswap swap service on Cloudflare Workers, exposing an MCP server and a mirrored REST API.
Status: Code-complete and fully tested (203 tests), not yet deployed.
Trust model: Single-operator custodial. The Worker holds one funded private key and signs swaps on your behalf. Anyone who mints a token can spend from that key, bounded only by the swap safety rails. Run it for yourself, not as a shared service.
Before you deploy: split the Durable Object migration tags into
v1/v2/v3and setCANONICAL_MCP_URIto your real Worker origin. Both steps are covered in the deploy how-to.
What it is
An AI agent (over the Model Context Protocol) or any HTTP client (over REST)
requests price quotes and executes ETH↔USDC swaps through the Uniswap Trading
API. A single OAuth 2.1 access token authorizes both surfaces; the /mcp and
/api handlers are mirrors of the same four operations.
Every swap is persisted in D1 and serialized through a Durable Object coordinator that enforces one in-flight swap at a time and applies drift, slippage, and balance safety rails before signing. Failed or interrupted swaps leave a durable row you can reconcile against the chain.
Architecture at a glance
| Surface | Path | Auth |
|---|---|---|
| MCP server | /mcp |
Bearer token, swap:read / swap:write scopes |
| REST mirror | /api |
Same token, same scopes |
| OAuth 2.1 + consent | /authorize, /token, /register |
Passphrase-gated consent |
The entry point (src/index.ts) is a @cloudflare/workers-oauth-provider
instance wrapping the MCP and REST handlers. It binds:
| Binding | Type | Purpose |
|---|---|---|
OAUTH_KV |
KV | OAuth grant + consent state |
DB |
D1 | The swaps table (drizzle) |
SwapCoordinator |
Durable Object | Serializes swap execution |
SwapMcpAgent |
Durable Object | Per-session MCP agent |
RateLimiter |
Durable Object | Passphrase-attempt budgets |
Surfaces
Each MCP tool has a REST mirror and a required scope:
| MCP tool | REST mirror | Scope |
|---|---|---|
get_quote |
POST /api/quote |
swap:read |
execute_swap |
POST /api/swap |
swap:write |
list_transactions |
GET /api/transactions |
swap:read |
get_transaction |
GET /api/transactions/:id |
swap:read |
For request/response schemas, the swaps data model, and the 11-code error
allowlist, see the API and data-model reference.
Quick start
pnpm install # install dependencies
pnpm dev # run the Worker locally with hot reload
pnpm test # run the full suite (203 tests, zero network)
The suite mocks all chain and Trading API I/O, so it needs no secrets. For the full local walkthrough (mint an operator token through the OAuth dance and run a mocked quote), follow the getting-started tutorial.
Documentation
The docs/ tree follows the Diátaxis framework:
Tutorial
- Getting started: stand up locally, mint a token, run a mocked quote.
How-to guides
- Configure secrets and deploy: set the four secrets, replace binding ids, apply migration tags, deploy.
- Run the one-time USDC approval: clear an
approval_requiredfailure on USDC→ETH swaps. - Reconcile a stranded submitted swap: resolve a swap stuck in
submitted.
Reference
- API and data model: MCP tools, REST endpoints,
swapsschema, error allowlist.
Explanation
- Architecture decisions: why OAuthProvider + McpAgent, the coordinator, the safety rails, and the custodial trade-offs.
Commands
| Command | What it does |
|---|---|
pnpm dev |
Local dev server (wrangler dev) |
pnpm deploy |
Deploy to Cloudflare (wrangler deploy --minify) |
pnpm test |
Run the vitest suite |
pnpm typecheck |
Type-check with tsc --noEmit |
pnpm lint |
Check formatting (prettier --check) |
pnpm format |
Apply formatting (prettier --write) |
pnpm cf-typegen |
Regenerate the CloudflareBindings type |
pnpm db:generate |
Generate a drizzle migration from the schema |
pnpm smoke |
Manual real-chain smoke script: hits a deployed env; never run by the suite |
Tech stack
Cloudflare Workers (Hono, Durable Objects, D1, KV), @cloudflare/workers-oauth-provider,
@modelcontextprotocol/sdk, the agents SDK, drizzle-orm, viem, and zod. Tested
with vitest and @cloudflare/vitest-pool-workers.
License
No license file yet.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。