石墨顿
  
LangGraph的声明式代理创建框架
Graphton在使用MCP工具创建LangGraph代理时消除了样板。在中创建生产就绪代理 3-10行,而不是100行+.
特性
- 声明性代理创建:最小样板-只需指定模型和行为
- 自动提示增强:代理自动了解可用功能(规划、文件系统、MCP工具)
- 通用MCP身份验证:支持任何具有静态和动态身份验证模式的MCP服务器配置
- 智能环路检测:自动检测并防止自治代理中的无限循环
- 生产就绪:适用于本地和远程LangGraph部署
- 类型安全配置:Pydantic验证,带有有用的错误消息
- IDE 支持:完整的自动补全和类型提示,以获得更好的开发人员体验
快速开始
安装
pip install graphton简单代理
只需3行即可创建基本代理:
from graphton import create_deep_agent
SYSTEM_PROMPT = """You are a helpful assistant that answers questions concisely.
When answering questions:
- Be direct and to the point
- Provide accurate information
- If you're not sure, say so
"""
# Create agent with just model and prompt
agent = create_deep_agent(
model="claude-sonnet-4.5",
system_prompt=SYSTEM_PROMPT,
)
# Invoke the agent
result = agent.invoke({
"messages": [{"role": "user", "content": "What is the capital of France?"}]
})使用MCP工具的代理
将MCP工具与动态每用户身份验证集成:
from graphton import create_deep_agent
import os
# Agent with dynamic MCP authentication
agent = create_deep_agent(
model="claude-sonnet-4.5",
system_prompt="You are a Planton Cloud assistant helping users manage cloud resources.",
# MCP integration with template variables
mcp_servers={
"planton-cloud": {
"transport": "streamable_http",
"url": "https://mcp.planton.ai/",
"headers": {
"Authorization": "Bearer {{USER_TOKEN}}" # Substituted at runtime
}
}
},
mcp_tools={
"planton-cloud": [
"list_organizations",
"search_cloud_resources",
"create_cloud_resource",
]
},
# Optional parameters
recursion_limit=150,
temperature=0.3,
)
# Invoke with user-specific token
result = agent.invoke(
{"messages": [{"role": "user", "content": "List my organizations"}]},
config={
"configurable": {
"USER_TOKEN": os.getenv("PLANTON_API_KEY")
}
}
)静态MCP配置
对于共享凭据,请使用静态配置(在创建时加载一次的工具):
agent = create_deep_agent(
model="claude-sonnet-4.5",
system_prompt="You are an API assistant.",
mcp_servers={
"public-api": {
"transport": "http",
"url": "https://api.example.com/mcp",
"headers": {
"X-API-Key": "hardcoded-key-123" # No templates = static
}
}
},
mcp_tools={
"public-api": ["search", "fetch"]
}
)
# Invoke without auth config - credentials already in config
result = agent.invoke(
{"messages": [{"role": "user", "content": "Search for Python"}]}
)代理与子代理
将复杂任务委托给具有隔离上下文的专用子代理:
from graphton import create_deep_agent
agent = create_deep_agent(
model="claude-sonnet-4.5",
system_prompt="You are a research coordinator that delegates specialized tasks.",
# Define specialized sub-agents
subagents=[
{
"name": "deep-researcher",
"description": "Conducts thorough research on complex topics with comprehensive analysis",
"system_prompt": "You are a research specialist. Conduct thorough research, cite sources, and provide comprehensive analysis.",
},
{
"name": "code-reviewer",
"description": "Reviews code for quality, security, and best practices",
"system_prompt": "You are a code review expert. Analyze code for bugs, security issues, and improvement opportunities.",
}
],
# Include general-purpose sub-agent for other tasks
general_purpose_agent=True,
)
# The agent can now delegate tasks to sub-agents
result = agent.invoke({
"messages": [{"role": "user", "content": "Research quantum computing and review the attached code"}]
})何时使用子代理:
- 可以委派的复杂多步骤任务
- 独立并行任务
- 需要集中推理而不需要上下文膨胀的任务
- 专业领域(研究、代码审查、数据分析)
优点:
- 上下文隔离:每个子代理都有自己的上下文窗口
- 代币效率:主代理获得简洁的摘要,而不是完整的任务历史记录
- 并行执行:同时启动多个子代理
- 专业化:具有特定领域工具的不同子代理
自动提示增强
Graphton会自动增强您的指令,并了解Deep Agents的功能。这确保了代理了解他们拥有什么工具以及何时使用它们。
什么得到增强
当您提供以下简单说明时:
agent = create_deep_agent(
model="claude-sonnet-4.5",
system_prompt="You are a helpful research assistant.",
)Graphton会自动添加以下内容的上下文:
- 规划系统:用于分解复杂的多步骤任务
- 文件系统:用于跨操作存储和管理信息
- MCP工具:配置后,了解特定域的功能
为什么这很重要
深度代理具有强大的内置工具(规划、文件系统、子代理),但除非代理知道它们的存在,否则它们不会有效地使用它们。Graphton通过自动通知代理可用功能来弥合这一差距。
控制和灵活性
- 默认情况下为自动:所有代理都会自动进行增强
- 冗余是可以的:如果您的说明中已经提到了计划或文件系统,则会出现一些重叠。这是有意的——LLM优雅地处理冗余,强化比缺少关键上下文要好
- 可以禁用:使用
auto_enhance_prompt=False按原样传递指令
# Disable enhancement if you've already included all context
agent = create_deep_agent(
model="claude-sonnet-4.5",
system_prompt="Detailed instructions with all tool context...",
auto_enhance_prompt=False, # Use prompt as-is
)文档
- simple_agent.py -无MCP的基本代理 - mcp_agent.py -动态MCP身份验证 - static_mcp_agent.py -静态MCP配置 - multi_auth_agent.py -具有混合身份验证的多个服务器
通用MCP身份验证
Graphton通过基于模板的令牌注入支持任何MCP服务器配置格式和身份验证方法:
动态模式 (与 {{VAR}} 模板):
- 模板替换自
config['configurable']在调用时 - 根据请求加载具有用户特定身份验证的工具
- 用于多租户系统或每用户令牌
静态模式 (无模板变量):
- 在代理创建时加载一次工具
- 零运行时开销
- 用于硬编码凭据或公共服务器
支持的身份验证方法:
- 承载令牌(OAuth、JWT)
- API密钥
- 基本认证
- 自定义头
- MCP服务器支持的任何身份验证格式
需求
- Python 3.11或更高版本
- 诗歌(发展)
许可证
Apache-2.0-见 许可证 详情
贡献
我们欢迎捐款!请看 贡献.md 用于开发设置和指南。
链接
- 仓库: https://github.com/plantoncloud/graphton
- 文档: https://github.com/plantoncloud/graphton#readme
- 问题: https://github.com/plantoncloud/graphton/issues
