名称: ydc-claude-agent-sdk-integration
描述: 将 Claude Agent SDK 与 You.com HTTP MCP 服务器集成,支持 Python 和 TypeScript。当开发者提及 Claude Agent SDK、Anthropic Agent SDK 或将 Claude 与 MCP 工具集成时使用。
许可证: MIT
compatibility: Python 3.10+ 或 TypeScript 5.2+ (v2 版本),Node.js 18+
元数据:
author: youdotcom-oss
category: sdk-integration
version: "1.0.0"
keywords: claude,anthropic,claude-agent-sdk,agent-sdk,mcp,you.com,integration,http-mcp,web-search,python,typescript
设置 Claude Agent SDK 与 You.com HTTP MCP 服务器的交互式工作流。
询问:选择编程语言
如果选择 TypeScript - 询问:SDK 版本
await using 语法安装包
pip install claude-agent-sdknpm install @anthropic-ai/claude-agent-sdk询问:环境变量
YDC_API_KEY 和 ANTHROPIC_API_KEY 吗?询问:文件位置
创建/更新文件
对于新建文件:
* 使用下方“完整模板”部分的完整模板代码
* 用户设置好 API 密钥后即可立即运行
对于现有文件:
* 将 HTTP MCP 服务器配置添加到其现有代码中
* Python 配置块:
```python
from claude_agent_sdk import query, ClaudeAgentOptions
options = ClaudeAgentOptions(
mcp_servers={
"ydc": {
"type": "http",
"url": "https://api.you.com/mcp",
"headers": {
"Authorization": f"Bearer {os.getenv('YDC_API_KEY')}"
}
}
},
allowed_tools=[
"mcp__ydc__you_search",
"mcp__ydc__you_express",
"mcp__ydc__you_contents"
]
)
```
typescript
const options = {
mcpServers: {
ydc: {
type: 'http' as const,
url: 'https://api.you.com/mcp',
headers: {
Authorization: `Bearer ${process.env.YDC_API_KEY}`
}
}
},
allowedTools: [
'mcp__ydc__you_search',
'mcp__ydc__you_express',
'mcp__ydc__you_contents'
]
};为新建文件使用这些完整模板。每个模板都已准备就绪,设置好 API 密钥即可运行。
"""
Claude Agent SDK 与 You.com HTTP MCP 服务器集成
使用 async/await 模式的 Python 实现
"""
import os
import asyncio
from claude_agent_sdk import query, ClaudeAgentOptions
# 验证环境变量
ydc_api_key = os.getenv("YDC_API_KEY")
anthropic_api_key = os.getenv("ANTHROPIC_API_KEY")
if not ydc_api_key:
raise ValueError(
"需要 YDC_API_KEY 环境变量。"
"获取密钥地址:https://you.com/platform/api-keys"
)
if not anthropic_api_key:
raise ValueError(
"需要 ANTHROPIC_API_KEY 环境变量。"
"获取密钥地址:https://console.anthropic.com/settings/keys"
)
async def main():
"""
示例:搜索 AI 新闻并从 You.com MCP 服务器获取结果
"""
# 配置 Claude Agent 并连接 HTTP MCP 服务器
options = ClaudeAgentOptions(
mcp_servers={
"ydc": {
"type": "http",
"url": "https://api.you.com/mcp",
"headers": {"Authorization": f"Bearer {ydc_api_key}"},
}
},
allowed_tools=[
"mcp__ydc__you_search",
"mcp__ydc__you_express",
"mcp__ydc__you_contents",
],
model="claude-sonnet-4-5-20250929",
)
# 向 Claude 发送查询,MCP 工具可用
async for message in query(
prompt="搜索本周最新的 AI 新闻",
options=options,
):
# 处理不同类型的消息
if message.type == "text":
print(message.content)
elif message.type == "tool_use":
print(f"\n[工具: {message.name}]")
print(f"输入: {message.input}")
elif message.type == "tool_result":
print(f"结果: {message.content}")
if __name__ == "__main__":
asyncio.run(main())
/**
* Claude Agent SDK 与 You.com HTTP MCP 服务器集成
* TypeScript v1 实现,基于生成器模式
*/
import { query } from '@anthropic-ai/claude-agent-sdk';
// 验证环境变量
const ydcApiKey = process.env.YDC_API_KEY;
const anthropicApiKey = process.env.ANTHROPIC_API_KEY;
if (!ydcApiKey) {
throw new Error(
'需要 YDC_API_KEY 环境变量。' +
'获取密钥地址:https://you.com/platform/api-keys'
);
}
if (!anthropicApiKey) {
throw new Error(
'需要 ANTHROPIC_API_KEY 环境变量。' +
'获取密钥地址:https://console.anthropic.com/settings/keys'
);
}
/**
* 示例:搜索 AI 新闻并从 You.com MCP 服务器获取结果
*/
async function main() {
// 向 Claude 发送查询,配置 HTTP MCP
const result = query({
prompt: '搜索本周最新的 AI 新闻',
options: {
mcpServers: {
ydc: {
type: 'http' as const,
url: 'https://api.you.com/mcp',
headers: {
Authorization: `Bearer ${ydcApiKey}`,
},
},
},
allowedTools: [
'mcp__ydc__you_search',
'mcp__ydc__you_express',
'mcp__ydc__you_contents',
],
model: 'claude-sonnet-4-5-20250929',
},
});
// 处理接收到的消息
for await (const msg of result) {
if (msg.type === 'text') {
console.log(msg.content);
} else if (msg.type === 'tool_use') {
console.log(`\n[工具: ${msg.name}]`);
console.log(`输入: ${JSON.stringify(msg.input, null, 2)}`);
} else if (msg.type === 'tool_result') {
console.log(`结果: ${msg.content}`);
}
}
}
main().catch(console.error);
/**
* Claude Agent SDK 与 You.com HTTP MCP 服务器集成
* TypeScript v2 实现,使用发送/接收模式
* 需要 TypeScript 5.2+ 以支持 'await using' 语法
*/
import { unstable_v2_createSession } from '@anthropic-ai/claude-agent-sdk';
// 验证环境变量
const ydcApiKey = process.env.YDC_API_KEY;
const anthropicApiKey = process.env.ANTHROPIC_API_KEY;
if (!ydcApiKey) {
throw new Error(
'需要 YDC_API_KEY 环境变量。' +
'获取密钥地址:https://you.com/platform/api-keys'
);
}
if (!anthropicApiKey) {
throw new Error(
'需要 ANTHROPIC_API_KEY 环境变量。' +
'获取密钥地址:https://console.anthropic.com/settings/keys'
);
}
/**
* 示例:搜索 AI 新闻并从 You.com MCP 服务器获取结果
*/
async function main() {
// 创建会话并配置 HTTP MCP
// 'await using' 确保在作用域退出时自动清理资源
await using session = unstable_v2_createSession({
mcpServers: {
ydc: {
type: 'http' as const,
url: 'https://api.you.com/mcp',
headers: {
Authorization: `Bearer ${ydcApiKey}`,
},
},
},
allowedTools: [
'mcp__ydc__you_search',
'mcp__ydc__you_express',
'mcp__ydc__you_contents',
],
model: 'claude-sonnet-4-5-20250929',
});
// 向 Claude 发送消息
await session.send('搜索本周最新的 AI 新闻');
// 接收并处理消息
for await (const msg of session.receive()) {
if (msg.type === 'text') {
console.log(msg.content);
} else if (msg.type === 'tool_use') {
console.log(`\n[工具: ${msg.name}]`);
console.log(`输入: ${JSON.stringify(msg.input, null, 2)}`);
} else if (msg.type === 'tool_result') {
console.log(`结果: ${msg.content}`);
}
}
}
main().catch(console.error);
所有模板都使用 You.com 的 HTTP MCP 服务器,简单易用:
Python:
mcp_servers={
"ydc": {
"type": "http",
"url": "https://api.you.com/mcp",
"headers": {
"Authorization": f"Bearer {ydc_api_key}"
}
}
}
TypeScript:
mcpServers: {
ydc: {
type: 'http' as const,
url: 'https://api.you.com/mcp',
headers: {
Authorization: `Bearer ${ydcApiKey}`
}
}
}
HTTP MCP 的优势:
- ✅ 无需本地安装
- ✅ 无状态请求/响应模型
- ✅ 始终保持最新版本
- ✅ 在所有环境中保持一致
- ✅ 生产就绪且可扩展
- ✅ 与现有 HTTP 基础设施兼容
配置完成后,Claude 可以发现并使用以下工具:
- mcp__ydc__you_search - 网页和新闻搜索
- mcp__ydc__you_express - 结合网页上下文的 AI 智能回答
- mcp__ydc__you_contents - 网页内容提取
需要两个 API 密钥:
# 添加到你的 .env 文件或 shell 配置文件中
export YDC_API_KEY="你的-you-api-key"
export ANTHROPIC_API_KEY="你的-anthropic-api-key"
获取你的 API 密钥:
- You.com: https://you.com/platform/api-keys
- Anthropic: https://console.anthropic.com/settings/keys
完成前请检查:
claude-agent-sdk (Python) 或 @anthropic-ai/claude-agent-sdk (TypeScript)YDC_API_KEY 和 ANTHROPIC_API_KEYhttps://api.you.com/mcp)Bearer ${YDC_API_KEY}Python:
python your-file.py
TypeScript:
# 使用 tsx(推荐用于快速测试)
npx tsx your-file.ts
# 或编译后运行
tsc your-file.ts && node your-file.js
# NPM
npm install @anthropic-ai/claude-agent-sdk
# Bun
bun add @anthropic-ai/claude-agent-sdk
# Yarn
yarn add @anthropic-ai/claude-agent-sdk
# pnpm
pnpm add @anthropic-ai/claude-agent-sdk
export YDC_API_KEY="你的-api-key"
获取密钥地址:https://you.com/platform/api-keys
export ANTHROPIC_API_KEY="你的-api-key"
获取密钥地址:https://console.anthropic.com/settings/keys
npm install -D typescript@latest
**解决方案 2:使用手动清理**
const session = unstable_v2_createSession({ /* 配置选项 */ });
try {
await session.send('你的查询');
for await (const msg of session.receive()) {
// 处理消息
}
} finally {
session.close();
}
**解决方案 3:改用 v1 SDK**
在设置过程中选择 v1 以获得更广泛的 TypeScript 兼容性。