MCP简介
如今,软件通常受益于与LLM的集成。当你构建一个人工智能应用程序时,你可能希望它与外部服务进行交互——发送Slack消息、读取Google Drive文件、查询数据库。这些集成被称为“工具”
MCP解决的问题
在MCP之前,您必须为要连接的每个服务编写自定义集成代码。
MCP解决了这个问题。 如果服务提供MCP服务器,您的应用程序将免费获得集成。
______________________________________________________________________
MCP之前
您编写此集成代码:
import { WebClient } from "@slack/web-api";
const slack = new WebClient(process.env.SLACK_TOKEN);
// Define tool for Claude
const slackTool = {
name: "send_slack_message",
description: "Send a message to Slack",
input_schema: {
type: "object",
properties: {
channel: { type: "string" },
text: { type: "string" }
}
}
};
const response = await anthropic.messages.create({
model: "claude-3-5-sonnet-20241022",
tools: [slackTool],
messages: [{ role: "user", content: "Send 'Hello' to #general" }]
});
// Handle the tool call yourself
if (response.stop_reason === "tool_use") {
const toolUse = response.content.find(c => c.type === "tool_use");
// You write the Slack integration logic
await slack.chat.postMessage({
channel: toolUse.input.channel,
text: toolUse.input.text
});
}
// Now repeat this for Google Drive...
// And again for your database...
// And again for every service...______________________________________________________________________
使用MCP
Slack提供服务器,您只需使用它:
import { Client } from "@modelcontextprotocol/sdk/client";
// Connect to Slack's MCP server (they wrote the integration)
const mcp = new Client();
await mcp.connect(slackMCPServer);
// Get tools automatically
const tools = await mcp.listTools();
const response = await anthropic.messages.create({
model: "claude-3-5-sonnet-20241022",
tools: tools, // Slack tools ready to use
messages: [{ role: "user", content: "Send 'Hello' to #general" }]
});
// Standard tool call - MCP handles Slack API for you
if (response.stop_reason === "tool_use") {
const toolUse = response.content.find(c => c.type === "tool_use");
await mcp.callTool({
name: toolUse.name,
arguments: toolUse.input
});
// MCP server does the actual Slack API call
}
// Add Drive? Just connect another MCP server, same code pattern
// Add database? Same thing______________________________________________________________________
关键要点
对于支持MCP的工具,您不必编写API集成。您不定义工具模式。您只需连接并使用该工具。
______________________________________________________________________
