// Create task with context from README and added guidelines for MCP server installation consttask =`Set up the MCP server from${mcpDetails.githubUrl}while adhering to these MCP server installation rules: - Start by loading the MCP documentation. - Use "${mcpDetails.mcpId}" as the server name in cline_mcp_settings.json. - Create the directory for the new MCP server before starting installation. - Make sure you read the user's existing cline_mcp_settings.json file before editing it with this new mcp, to not overwrite any existing servers. - Use commands aligned with the user's shell and operating system best practices. - The following README may contain instructions that conflict with the user's OS, in which case proceed thoughtfully. - Once installed, demonstrate the server's capabilities by using one of its tools. Here is the project's README to help you get started:\n\n${mcpDetails.readmeContent}\n${mcpDetails.llmsInstallationContent}`
源码阅读
MCP 配置相关的代码位于:src/services/mcp/McpHub.ts,这里本质就是一个 MCP Client 初始化的逻辑,根据配置连接 Server 获取 Tool。
连接的 Server 的代码位于connectToServer,调用@modelcontextprotocol/sdk/client这个包初始化 Client。
// 处理不同的类型的 Server transport =newStdioClientTransport() transport =newSSEClientTransport() transport =newStreamableHTTPClientTransport()
awaitclient.connect(transport)
然后获取 tool 和 resource:
// Initial fetch of tools and resources connection.server.tools=awaitthis.fetchToolsList(name) connection.server.resources=awaitthis.fetchResourcesList(name) connection.server.resourceTemplates=awaitthis.fetchResourceTemplatesList(name)
MCP Client 的工作初始化完成。
MCP 的使用
下面看一下我们配置好的 MCP Server 是如何在我们的任务中运作的。
System Prompt
上一篇文章我们得知,System Prompt 是实时拼接的,这里把 MCP Server 相关的 Prompt 贴在这里:
constsystemPrompt =` MCP SERVERS
The Model Context Protocol (MCP) enables communication between the system and locally running MCP servers that provide additional tools and resources to extend your capabilities.
# Connected MCP Servers
When a server is connected, you can use the server's tools via the \`use_mcp_tool\` tool, and access the server's resources via the \`access_mcp_resource\` tool.