Node.js人工智能代理+MCP(TypeScript)
这个包在TypeScript中实现了一个AI代理工作流和几个MCP服务器。它反映了Python在 ../python/ 但使用Node.js生态系统。
它的作用
- LinkedIn摘要生成器管道:给定一个人的名字,代理通过MCP工具找到他们的LinkedIn URL,抓取个人资料详细信息,然后要求法学硕士提供一个简短的摘要和两个有趣的事实。
- MCP服务器:用于工具演示的数学(stdio)和天气(SSE/http流)的MCP服务器示例。
- MCP客户端:连接到这些MCP服务器并使用工具运行代理的小型客户端。
技术栈
- 运行时/语言:Node.js+TypeScript
- LLM框架:
langchain,@langchain/openai,@langchain/langgraph - 主控程序:
@langchain/mcp-adapters对于客户来说,fastmcp对于服务器 - 验证/解析:
zod,@langchain/core/output_parsers - 超文本传输协议:
axios - 配置:
dotenv
项目结构(关键文件)
src/index.ts:端到端LinkedIn摘要链(提示→ LLM → 解析器)。用途lookup从agents/linkedinAgent和scrapeLinkedinProfile从third_parties/linkedin.src/agents/linkedinAgent.ts:与MCP服务器通信的LangChain工具代理(src/mcp_server/profiles.ts)viaMultiServerMCPClient获取给定名称的LinkedIn URL。src/mcp_server/profiles.ts:FastMCP服务器暴露:
- 提示: profile_prompt - 工具: profile_url (使用Tavily搜索LinkedIn URL) - 资源模板: profile://users/{name}
src/_fastmcp/servers/mathServer.ts:通过stdio的FastMCP数学服务器。src/_fastmcp/servers/weatherServer.ts:基于HTTP流的FastMCP天气服务器(SSE)。src/_fastmcp/langchainClient.ts:连接到数学(stdio)和天气(SSE)服务器,构建一个React风格的代理,并对其进行查询。src/third_parties/linkedin.ts:通过Scrapin API(或模拟要点URL)抓取LinkedIn数据。src/outputParsers.ts:StructuredOutputParser总结器输出采用Zod模式。
需求
创建一个 .env 此文件夹中的文件包含:
OPENAI_API_KEY=sk-...
OPENAI_MODEL_NAME=gpt-4o-mini
TAVILY_API_KEY=...
SCRAPIN_API_KEY=...安装依赖项:
npm install如何跑步
1) 端到端LinkedIn摘要
跑 src/index.ts 其中:通过MCP支持的代理获取LinkedIn URL→ 刮擦轮廓→ 总结。
npm start笔记:
- 默认名称是硬编码的
src/index.ts(main("Arif Hidayatullah")).编辑该内容以更改目标人员。 - 需要
OPENAI_*,TAVILY_API_KEY,以及SCRAPIN_API_KEY在.env.
2) 仅运行LinkedIn查找代理
npm run linkedin-agent这使用 src/agents/linkedinAgent.ts 仅返回LinkedIn个人资料URL。
3) 手动运行MCP配置文件服务器(可选)
LinkedIn代理将自动生成此服务器,但您可以运行它进行测试:
npm run mcp-profiles4) MCP演示:数学(stdio)+天气(SSE)
客户在 src/_fastmcp/langchainClient.ts 连接到两个服务器:
- math:作为stdio上的子进程生成
- 天气:需要外部SSE服务器
http://localhost:8080/sse
步骤:
- 启动天气SSE服务器:
npm run mcp-weather- 在另一个终端中,运行客户端:
npm run mcp-client可选:独立运行数学服务器进行调试:
npm run mcp-math脚本(来自 package.json)
start:node -r ts-node/register --env-file=.env src/index.tslinkedin-agent:运行src/agents/linkedinAgent.tsmcp-profiles:运行src/mcp_server/profiles.ts(stdio)mcp-math:运行src/_fastmcp/servers/mathServer.ts(stdio)mcp-weather:运行src/_fastmcp/servers/weatherServer.ts(SSE开启http://localhost:8080/sse)mcp-client:运行src/_fastmcp/langchainClient.ts(预计天气SSE服务器正在运行)
Windows路径注释
一些演示客户端引用了仓库的绝对Windows路径,例如 src/agents/linkedinAgent.ts 和 src/_fastmcp/langchainClient.ts。如果将仓库克隆到其他位置,请更新这些位置 args 路径匹配您的本地路径或重构为相对路径。
故障排除
- 401/权限错误:检查
OPENAI_API_KEY,TAVILY_API_KEY,以及SCRAPIN_API_KEY. - 未找到模型:确保
OPENAI_MODEL_NAME是您的密钥可以访问的模型。 - 天气客户端无法连接:确认
npm run mcp-weather正在运行并且客户端使用http://localhost:8080/sse. - 路径错误:如上所述更新硬编码绝对路径。
