Build agent-facing web experiences with ATXP-based authentication, following the ClawDirect pattern. Use this skill when building websites that AI agents interact with via MCP tools, implementing cookie-based agent auth, or creating agent skills for web apps. Provides templates using @longrun/turtle, Express, SQLite, and ATXP.
构建基于ATXP认证的面向代理的Web体验。
参考实现: https://github.com/napoleond/clawdirect
ATXP(代理交易协议)使AI代理能够进行身份认证并为服务付费。在构建面向代理的网站时,ATXP提供:
完整ATXP详情:https://skills.sh/atxp-dev/cli/atxp
代理通过两种方式与您的网站交互:
基于Cookie的认证模式连接了这两种方式:代理通过MCP获取认证Cookie,然后在浏览时使用它。
重要提示:代理浏览器通常无法直接设置HTTP-only Cookie。推荐模式是代理在查询字符串中传递Cookie值(例如,?myapp_cookie=XYZ),然后由服务器设置Cookie并重定向到干净的URL。
┌──────────────────────────────────────────────────────────────────┐
│ AI代理 │
│ ┌─────────────────────┐ ┌─────────────────────────┐ │
│ │ 浏览器工具 │ │ MCP客户端 │ │
│ │ (访问网站) │ │ (调用工具) │ │
│ └─────────┬───────────┘ └───────────┬─────────────┘ │
└────────────┼─────────────────────────────────┼──────────────────┘
│ │
▼ ▼
┌────────────────────────────────────────────────────────────────┐
│ 您的应用程序 │
│ ┌─────────────────────┐ ┌─────────────────────────┐ │
│ │ Web服务器 │ │ MCP服务器 │ │
│ │ (Express) │ │ (@longrun/turtle) │ │
│ │ │ │ │ │
│ │ - 提供UI │ │ - yourapp_cookie │ │
│ │ - Cookie认证 │ │ - yourapp_action │ │
│ └─────────┬───────────┘ └───────────┬─────────────┘ │
│ │ │ │
│ └──────────┬─────────────────┘ │
│ ▼ │
│ ┌─────────────────┐ │
│ │ SQLite │ │
│ │ auth_cookies │ │
│ └─────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
使用所需技术栈初始化Node.js项目:
bash
mkdir my-agent-app && cd my-agent-app
npm init -y
npm install @longrun/turtle @atxp/server @atxp/express better-sqlite3 express cors dotenv zod
npm install -D typescript @types/node @types/express @types/cors @types/better-sqlite3 tsx
创建 tsconfig.json:
json
{
compilerOptions: {
target: ES2022,
module: NodeNext,
moduleResolution: NodeNext,
outDir: dist,
rootDir: src,
strict: true,
esModuleInterop: true,
skipLibCheck: true
},
include: [src//*]
}
创建 .env:
FUNDINGDESTINATIONATXP=<您的atxp账户>
PORT=3001
创建 src/db.ts:
typescript
import Database from better-sqlite3;
import crypto from crypto;
const DBPATH = process.env.DBPATH || ./data.db;
let db: Database.Database;
export function getDb(): Database.Database {
if (!db) {
db = new Database(DB_PATH);
db.pragma(journal_mode = WAL);
// 认证Cookie表 - 将Cookie映射到ATXP账户
db.exec(
CREATE TABLE IF NOT EXISTS auth_cookies (
cookie_value TEXT PRIMARY KEY,
atxp_account TEXT NOT NULL,
createdat DATETIME DEFAULT CURRENTTIMESTAMP
)
);
// 在此添加您的应用表
}
return db;
}
export function createAuthCookie(atxpAccount: string): string {
const cookieValue = crypto.randomBytes(32).toString(hex);
getDb().prepare(
INSERT INTO authcookies (cookievalue, atxp_account)
VALUES (?, ?)
).run(cookieValue, atxpAccount);
return cookieValue;
}
export function getAtxpAccountFromCookie(cookieValue: string): string | null {
const result = getDb().prepare(
SELECT atxpaccount FROM authcookies WHERE cookie_value = ?
).get(cookieValue) as { atxp_account: string } | undefined;
return result?.atxp_account || null;
}
创建 src/tools.ts:
typescript
import { defineTool } from @longrun/turtle;
import { z } from zod;
import { requirePayment, atxpAccountId } from @atxp/server;
import BigNumber from bignumber.js;
import { createAuthCookie } from ./db.js;
// Cookie工具 - 代理调用此工具获取浏览器认证
export const cookieTool = defineTool(
myapp_cookie, // 将myapp替换为您的应用名称
获取用于浏览器使用的认证Cookie。设置此Cookie以在使用Web界面时进行认证。,
z.object({}),
async () => {
// 免费但需要ATXP认证
const accountId = atxpAccountId();
if (!accountId) {
throw new Error(需要认证);
}
const cookie = createAuthCookie(accountId);
return JSON.stringify({
cookie,
instructions: 要在浏览器中认证,请导航到 https://your-domain.com?myappcookie=
});
}
);
// 示例付费工具
export const paidActionTool = defineTool(
myapp_action,
执行某个操作。费用:$0.10,
z.object({
input: z.string().describe(操作的输入)
}),
async ({ input }) => {
await requirePayment({ price: new BigNumber(0.10) });
const accountId = atxpAccountId();
if (!accountId) {
throw new Error(需要认证);
}
// 您的操作逻辑在此
return JSON.stringify({ success: true, input });
}
);
export const allTools = [cookieTool, paidActionTool];
创建 src/api.ts:
typescript
import { Router, Request, Response } from express;
import { getAtxpAccountFromCookie } from ./db.js;
export const apiRouter = Router();
// 提取Cookie的辅助函数
function getCookieValue(req: Request, cookieName: string): string | null {
const cookieHeader = req.headers.cookie;
if (!cookieHeader) return null;
const cookies = cookieHeader.split(;).map(c => c.trim());
for (const cookie of cookies) {
if (cookie.startsWith(${cookieName}=)) {
return cookie.substring(cookieName.length + 1);
}
}
return null;
}
// 需要Cookie认证的中间件
function requireCookieAuth(req: Request, res: Response, next: Function) {
const cookieValue = getCookieValue(req, myapp_cookie);
if (!cookieValue) {
res.status(401).json({
error: 需要认证,
message: 使用myapp_cookie MCP工具获取认证Cookie
});
return;
}
const atxpAccount = getAtxpAccountFromCookie(cookieValue);
if (!atxpAccount) {
res.status(401).json({
error: 无效的Cookie,
message: 您的Cookie无效或已过期。请通过MCP工具获取新的Cookie。
});
return;
}
// 将账户附加到请求中供处理程序使用
(req as any).atxpAccount = atxpAccount;
next();
}
// 公共端点(无需认证)
api
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 clawdirect-dev-1776377932 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 clawdirect-dev-1776377932 技能
skillhub install clawdirect-dev-1776377932
文件大小: 5.83 KB | 发布时间: 2026-4-17 14:40