Initial commit: jina-reader-agent-v6 with 1 tools - src/server/mcp_server.py
This commit is contained in:
@@ -0,0 +1,132 @@
|
|||||||
|
"""
|
||||||
|
MCP 服务器 - jina-reader-agent-v6-fbef49
|
||||||
|
|
||||||
|
Agent with 1 external tools
|
||||||
|
使用 Pydantic AI 和 FastMCP 框架。
|
||||||
|
自动生成时间: 2026-01-30T18:12:32.140675
|
||||||
|
"""
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from mcp.server.fastmcp import FastMCP
|
||||||
|
from pydantic_ai import Agent
|
||||||
|
|
||||||
|
# ==================== 配置 ====================
|
||||||
|
|
||||||
|
# LiteLLM Gateway 配置
|
||||||
|
_BASE_URL = os.getenv('OPENAI_BASE_URL',
|
||||||
|
os.getenv('LLM_BASE_URL', 'https://litellm.graystone-fb459c5d.southeastasia.azurecontainerapps.io/v1'))
|
||||||
|
_API_KEY = os.getenv('OPENAI_API_KEY', 'sk')
|
||||||
|
|
||||||
|
os.environ.setdefault('OPENAI_API_KEY', _API_KEY)
|
||||||
|
os.environ.setdefault('OPENAI_BASE_URL', _BASE_URL)
|
||||||
|
|
||||||
|
# 模型名称(pydantic_ai 需要 openai: 前缀)
|
||||||
|
def _get_model_name() -> str:
|
||||||
|
model = os.getenv('MODEL_NAME', os.getenv('LITELLM_MODEL', 'taiji/gpt-4o-mini'))
|
||||||
|
return model if ':' in model else f'openai:{model}'
|
||||||
|
|
||||||
|
MODEL_NAME = _get_model_name()
|
||||||
|
|
||||||
|
# ==================== MCP 服务器 ====================
|
||||||
|
|
||||||
|
server = FastMCP('jina-reader-agent-v6-fbef49')
|
||||||
|
|
||||||
|
# 系统提示词
|
||||||
|
SYSTEM_PROMPT = """你是 jina-reader-agent-v6-fbef49,一个专业的 AI 智能助手。
|
||||||
|
|
||||||
|
## 角色定位
|
||||||
|
Agent with 1 external tools
|
||||||
|
|
||||||
|
## 可用工具
|
||||||
|
- jina_reader_v6: 使用 Jina Reader API 爬取网页内容,返回干净的 Markdown 格式
|
||||||
|
|
||||||
|
## 工作原则
|
||||||
|
1. 理解用户意图:仔细分析用户的请求,确保准确理解需求
|
||||||
|
2. 选择合适工具:根据需求选择最合适的工具来完成任务
|
||||||
|
3. 清晰反馈:以用户友好的方式呈现结果
|
||||||
|
4. 错误处理:遇到问题时提供有用的错误信息和建议
|
||||||
|
|
||||||
|
## 响应格式
|
||||||
|
- 对于数据查询:返回结构化的 JSON 数据
|
||||||
|
- 对于操作请求:返回操作状态和结果
|
||||||
|
- 始终使用中文与用户交流(除非用户使用其他语言)"""
|
||||||
|
|
||||||
|
|
||||||
|
def get_agent() -> Agent:
|
||||||
|
"""创建 Agent 实例(每次调用使用最新的 API Key)"""
|
||||||
|
return Agent(MODEL_NAME, system_prompt=SYSTEM_PROMPT)
|
||||||
|
|
||||||
|
|
||||||
|
# ==================== MCP 工具定义 ====================
|
||||||
|
|
||||||
|
@server.tool()
|
||||||
|
async def jina_reader_v6(url: Optional[str] = None) -> str:
|
||||||
|
"""
|
||||||
|
使用 Jina Reader API 爬取网页内容,返回干净的 Markdown 格式
|
||||||
|
|
||||||
|
Args:
|
||||||
|
url: 要爬取的网页URL
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
API 响应结果 (JSON 格式)
|
||||||
|
"""
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
url = "https://r.jina.ai/"
|
||||||
|
headers = {"Authorization": f"Bearer {os.getenv('TOOL_API_KEY', 'None')}" }
|
||||||
|
params = {"url": url}
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with httpx.AsyncClient(timeout=30) as client:
|
||||||
|
response = await client.request(
|
||||||
|
method="POST",
|
||||||
|
url=url,
|
||||||
|
headers=headers,
|
||||||
|
params={k: v for k, v in params.items() if v is not None}
|
||||||
|
)
|
||||||
|
|
||||||
|
if response.status_code == 200:
|
||||||
|
return json.dumps({
|
||||||
|
"success": True,
|
||||||
|
"data": response.json() if response.headers.get("content-type", "").startswith("application/json") else response.text
|
||||||
|
}, ensure_ascii=False, indent=2)
|
||||||
|
else:
|
||||||
|
return json.dumps({
|
||||||
|
"success": False,
|
||||||
|
"status_code": response.status_code,
|
||||||
|
"error": response.text[:500]
|
||||||
|
}, ensure_ascii=False)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
# 使用 AI Agent 作为后备
|
||||||
|
result = await get_agent().run(f"请帮我处理这个请求: ['url: Optional[str] = None']")
|
||||||
|
return json.dumps({
|
||||||
|
"success": True,
|
||||||
|
"source": "ai_agent",
|
||||||
|
"result": result.output
|
||||||
|
}, ensure_ascii=False, indent=2)
|
||||||
|
|
||||||
|
|
||||||
|
# ==================== 工具映射(供 API 使用)====================
|
||||||
|
|
||||||
|
TOOL_MAP = {
|
||||||
|
'jina_reader_v6': jina_reader_v6,
|
||||||
|
}
|
||||||
|
|
||||||
|
TOOL_LIST = [
|
||||||
|
{
|
||||||
|
"name": "jina_reader_v6",
|
||||||
|
"description": "使用 Jina Reader API 爬取网页内容,返回干净的 Markdown 格式",
|
||||||
|
"inputSchema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {"url": {"type": "string", "description": "要爬取的网页URL"}},
|
||||||
|
"required": []
|
||||||
|
}
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
server.run()
|
||||||
Reference in New Issue
Block a user