一个基于 TypeScript 的 NodeJS 智能代理核心库,集成了 OpenAI API 和 MCP (Model Context Protocol) 支持。
- TypeScript 支持:完整的类型定义和严格的类型检查
- OpenAI 集成:支持 OpenAI 和 Azure OpenAI 服务
- MCP 协议:支持 Model Context Protocol 服务器集成
- 事件驱动:基于 EventEmitter 的异步事件处理
- 工具调用:支持函数调用和工具执行
- 环境配置:支持 dotenv 环境变量管理
- 代码规范:集成 ESLint + Prettier 代码格式化
# 克隆项目
git clone <repository-url>
cd common-agent-core
# 安装依赖(使用 pnpm)
pnpm install
# 构建项目
pnpm build创建 .env 文件并配置必要的环境变量:
OPENAI_API_KEY=your_openai_api_key
OPENAI_BASE_URL=https://api.openai.com/v1
OPENAI_MODEL_NAME=gpt-4import { Agent } from './src/index';
async function main() {
const agent = new Agent({
options: {
tool_type: 'function_call',
},
});
// 注册 MCP 服务器
await agent.registerMcpServers({
mcpServers: {
'filesystem': {
command: 'npx',
args: ['-y', '@modelcontextprotocol/server-filesystem', '/path/to/directory'],
env: {},
},
},
});
// 监听事件
agent.on('reasoning', data => {
console.log('推理过程:', data.toString());
});
agent.on('message', data => {
console.log('消息:', data.toString());
});
agent.on('toolCall', data => {
console.log('工具调用:', JSON.stringify(data, null, 2));
});
agent.on('toolResult', data => {
console.log('工具结果:', JSON.stringify(data, null, 2));
});
// 运行对话
await agent.run('请帮我分析当前目录下的文件');
// 清理资源
await agent.destory();
}
main().catch(console.error);支持多种 MCP 服务器:
// 文件系统服务器
{
'filesystem': {
command: 'npx',
args: ['-y', '@modelcontextprotocol/server-filesystem', '/path/to/directory'],
env: {},
}
}
// 自定义服务器
{
'custom-server': {
command: 'node',
args: ['./path/to/server.js'],
env: { NODE_ENV: 'production' },
}
}# 开发模式
pnpm dev
# 构建项目
pnpm build
# 类型检查
pnpm type-check
# 代码检查
pnpm lint
# 自动修复代码格式
pnpm lint:fix
# 格式化代码
pnpm format
# 检查代码格式
pnpm format:check
# 清理构建文件
pnpm cleansrc/
├── index.ts # 主入口文件
├── mcp/
│ ├── client.ts # MCP 客户端
│ └── demo-servers/ # 示例服务器
├── providers/
│ └── openai.ts # OpenAI 提供者
├── prompts/
│ └── setup.md # 系统提示词
├── types.ts # 类型定义
└── utils.ts # 工具函数
- Node.js: >= 18.0.0
- TypeScript: 5.9.2
- OpenAI SDK: 5.19.1
- MCP SDK: @modelcontextprotocol/sdk
- 包管理: pnpm >= 8.0.0
new Agent(config?: ChatConfig)registerMcpServers(config: { mcpServers: McpServersConfig }): 注册 MCP 服务器run(message: string, options?: ChatOptions): 运行对话destory(): 清理资源
reasoning: 推理过程数据message: 消息数据toolCall: 工具调用信息toolResult: 工具执行结果finish: 对话完成error: 错误信息
- Fork 项目
- 创建特性分支 (
git checkout -b feature/AmazingFeature) - 提交更改 (
git commit -m 'Add some AmazingFeature') - 推送到分支 (
git push origin feature/AmazingFeature) - 打开 Pull Request
本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情。