自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

從源碼分析 MCP 的實(shí)現(xiàn)和使用

開(kāi)發(fā) 前端
MCP(Model Context Protocol)是最近非常熱門(mén)的詞,本文從實(shí)現(xiàn)和使用兩個(gè)方面介紹 MCP 的內(nèi)容。

MCP 是什么?

官方的定義如下:

MCP is an open protocol that standardizes how applications provide context to LLMs. Think of MCP like a USB-C port for AI applications. Just as USB-C provides a standardized way to connect your devices to various peripherals and accessories, MCP provides a standardized way to connect AI models to different data sources and tools.

從實(shí)現(xiàn)來(lái)看,MCP 定義了一套通用的 API 用于 MCP 客戶端和服務(wù)器通信,MCP 開(kāi)發(fā)者只需要基于 MCP 提供的 Server 注冊(cè)自己的工具、Prompt,就可以通過(guò) MCP 提供的 Client 獲取并調(diào)用這些能力,官網(wǎng)給的圖如下。

MCP 提供的客戶端和服務(wù)器實(shí)現(xiàn)了通用的能力,比如如何通信,如何獲取工具等,具體有哪些能力(比如由哪里工具)是由 Server 開(kāi)發(fā)者定義的,我們可以自己開(kāi)發(fā)工具,也可以使用第三方開(kāi)源的。但如何使用這些能力是用使用者決定的,比如我們開(kāi)發(fā)一個(gè)基于 Redis 的 MCP Server,那么我們就可以通過(guò) MCP Client 從這個(gè) Server 從 Redis 里獲取信息,但是如何使用這些信息是自己定義的,一般來(lái)說(shuō)就是輸入給大模型。

了解了 MCP Server 和 Client 架構(gòu)后,接著看看 MCP 在實(shí)際場(chǎng)景中的使用架構(gòu)。下圖(來(lái)自《 MCP 是什么,現(xiàn)狀和未來(lái)?)描述了 MCP 在用戶和大模型通信過(guò)程中的位置和作用。

上圖的流程大致如下。

  • 用戶通過(guò) MCP Client 從 MCP Server 獲取可用的能力,比如工具、Prompt。
  • 把工具和用戶輸入傳入大模型,大模型返回時(shí)會(huì)告訴用戶可以使用哪些工具去獲取更多信息。
  • 用戶再次通過(guò) MCP Client 請(qǐng)求 MCP Server,讓 MCP Server 執(zhí)行對(duì)應(yīng)的工具獲取數(shù)據(jù)。
  • 用戶從 MCP Server 獲取數(shù)據(jù)后,再次訪問(wèn)大模型,大模型就可以利用工具的信息更好地回解決用戶的問(wèn)題。

MCP 的使用例子

了解了 MCP 的一些基礎(chǔ)概念后,來(lái)看一下 MCP 使用的例子。

import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
import express from 'express';
import { z } from 'zod';
import { CallToolRequestSchema, ListToolsRequestSchema, } from "@modelcontextprotocol/sdk/types.js";
const { Request, Response } = express;
const GetArgumentsSchema = z.object({
  key: z.string(),
});
// 模擬 redis
const memory = {"hello": "world"};
// 創(chuàng)建 MCP Server
const server = new Server({
  name: "example-server",
  version: "1.0.0",
},{
  capabilities: {
    tools: {},
  }
});
// 注冊(cè)獲取工具 API 路由
server.setRequestHandler(ListToolsRequestSchema, async () => {
  return {
      tools: [
          {
              name: "get",
              description: "Get value by key from kv",
              inputSchema: {
                  type: "object",
                  properties: {
                      key: {
                          type: "string",
                          description: "",
                      },
                  },
                  required: ["key"],
              },
          },
      ],
  };
});
// 注冊(cè)執(zhí)行工具 API 路由
server.setRequestHandler(CallToolRequestSchema, async (request) => {
  console.log(request.params)
  const { name, arguments: args } = request.params;
  const { key } = GetArgumentsSchema.parse(args);
  return {
    content: [
        {
            type: "text",
            text: `${memory[key]}`,
        },
    ],
};
});
// 啟動(dòng) HTTP 服務(wù)器
const app = express();
const transports: {[sessionId: string]: SSEServerTransport} = {};
// SSE 路由
app.get("/sse", async (_: Request, res: Response) => {
  // 創(chuàng)建一個(gè) SSE 通道,meesages 為后續(xù)通信的路由
  const transport = new SSEServerTransport('/messages', res);
  transports[transport.sessionId] = transport;
  res.on("close", () => {
    delete transports[transport.sessionId];
  });
  await server.connect(transport);
});
// 通信路由
app.post("/messages", async (req: Request, res: Response) => {
  const sessionId = req.query.sessionId as string;
  const transport = transports[sessionId];
  if (transport) {
    // 處理請(qǐng)求,處理完畢后通過(guò)上面的 sse res 進(jìn)行響應(yīng)
    await transport.handlePostMessage(req, res);
  } else {
    res.status(400).send('No transport found for sessionId');
  }
});
app.listen(3001);

MCP Server 并不是一個(gè)傳統(tǒng)的 HTTP Server,它只是負(fù)責(zé)處理來(lái)自客戶端的請(qǐng)求,并不處理 HTTP 協(xié)議本身,其中,Server 的 setRequestHandler API 用于注冊(cè)路由和處理函數(shù),第一個(gè) setRequestHandler 注冊(cè)了獲取工具列表 API,第二個(gè) setRequestHandler 注冊(cè)了執(zhí)行工具的 API,客戶端一般先獲取工具列表,然后再調(diào) API 執(zhí)行具體的工具。創(chuàng)建 MCP Server 后,還需要?jiǎng)?chuàng)建一個(gè)通信管道用戶接收客戶端的請(qǐng)求,這里是 SSE 通道,SSE 會(huì)接收到來(lái)自 HTTP 服務(wù)器的請(qǐng)求,然后傳遞給 MCP Server,MCP Server 再通過(guò)通道把處理結(jié)果返回給客戶端。

接著看客戶端的例子。

import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
async function runClient() {
    const client = new Client(
      {
        name: "mcp-typescript test client",
        version: "0.1.0",
      },
    );
  
  
    const clientTransport = new SSEClientTransport(new URL("http://localhost:3001/sse"));
  
    await client.connect(clientTransport);
    const tools = await client.listTools()  
    console.log(tools)
    const resp = await client.callTool({
      name: "get",
      arguments: {
        key: "hello"
      }
    })
    console.log(resp);
    await client.close();
  }
  runClient();


執(zhí)行上面代碼會(huì)首先從 MCP 客戶端獲取工具列表,然后執(zhí)行 get 命令獲取數(shù)據(jù),客戶端和服務(wù)端的架構(gòu)類似,就不再介紹。

圖片

MCP 的實(shí)現(xiàn)

Transport

MCP 客戶端和服務(wù)器需要通信來(lái)完成數(shù)據(jù)的交互,而通信就需要一個(gè)通道,所以先看一下通道的實(shí)現(xiàn)。MCP 支持通過(guò)本地、SSE、等方式來(lái)通信,通過(guò) Transport 實(shí)現(xiàn)了抽象。

interface Transport {
  // 初始化通道
  start(): Promise<void>;
  // 發(fā)送信息或響應(yīng)
  send(message: JSONRPCMessage): Promise<void>;
  
  close(): Promise<void>;
  onclose?: () => void;
  onerror?: (error: Error) => void;
  // 處理收到的請(qǐng)求或響應(yīng)
  onmessage?: (message: JSONRPCMessage) => void;
  // 連接對(duì)應(yīng)的回話 ID
  sessionId?: string;
}

Transport 可以用于客戶端或服務(wù)端,只要實(shí)現(xiàn)了上面的方法就行。

我們來(lái)看一個(gè)基于內(nèi)存的通信管道的實(shí)現(xiàn),類似 Unix 管道。

class InMemoryTransport implements Transport {
  private _otherTransport?: InMemoryTransport;
  private _messageQueue: JSONRPCMessage[] = [];
  onclose?: () => void;
  onerror?: (error: Error) => void;
  onmessage?: (message: JSONRPCMessage) => void;
  sessionId?: string;
  static createLinkedPair(): [InMemoryTransport, InMemoryTransport] {
    // 創(chuàng)建兩個(gè)管道,互相關(guān)聯(lián)
    const clientTransport = new InMemoryTransport();
    const serverTransport = new InMemoryTransport();
    clientTransport._otherTransport = serverTransport;
    serverTransport._otherTransport = clientTransport;
    return [clientTransport, serverTransport];
  }
  // 初始化管道,如果之前已經(jīng)存在數(shù)據(jù),則消費(fèi)
  async start(): Promise<void> {
    while (this._messageQueue.length > 0) {
      const message = this._messageQueue.shift();
      if (message) {
        this.onmessage?.(message);
      }
    }
  }
  async close(): Promise<void> {
    const other = this._otherTransport;
    this._otherTransport = undefined;
    await other?.close();
    this.onclose?.();
  }
  // 發(fā)送數(shù)據(jù)
  async send(message: JSONRPCMessage): Promise<void> {
    if (!this._otherTransport) {
      throw new Error("Not connected");
    }
    // 如果還沒(méi)有消費(fèi)者,則先緩存
    if (this._otherTransport.onmessage) {
      this._otherTransport.onmessage(message);
    } else {
      this._otherTransport._messageQueue.push(message);
    }
  }
}

在實(shí)際場(chǎng)景中,我們一般使用 SSE 或 Websocket 來(lái)實(shí)現(xiàn)通信,下面看一下  Server SSE Transport的實(shí)現(xiàn)。

class SSEServerTransport implements Transport {
  private _sseResponse?: ServerResponse;
  private _sessionId: string;
  onclose?: () => void;
  onerror?: (error: Error) => void;
  // 上層設(shè)置
  onmessage?: (message: JSONRPCMessage) => void;
 
  // 初始化時(shí)記錄 SSE 連接對(duì)應(yīng)的響應(yīng)對(duì)象,以及用于通信的 endpoint 路由
  constructor(
    private _endpoint: string,
    private res: ServerResponse,
  ) {
    this._sessionId = randomUUID();
  }
  async start(): Promise<void> {
    // 返回 text/event-stream 類型的響應(yīng)頭,表示這是一個(gè) SSE 連接,后續(xù)可以基于這個(gè)連接進(jìn)行數(shù)據(jù)推送
    this.res.writeHead(200, {
      "Content-Type": "text/event-stream",
      "Cache-Control": "no-cache",
      Connection: "keep-alive",
    });
    // 返回 endpoint 信息,作為 POST 請(qǐng)求的 url 路徑
    this.res.write(
      `event: endpoint\ndata: ${encodeURI(this._endpoint)}?sessinotallow=${this._sessionId}\n\n`,
    );
    // 記錄 SSE 連接對(duì)應(yīng)的響應(yīng)對(duì)象
    this._sseResponse = this.res;
  }
  // 處理 POST 請(qǐng)求,解析請(qǐng)求體,將請(qǐng)求體并通過(guò) SSE 響應(yīng)對(duì)象把請(qǐng)求結(jié)果發(fā)送給客戶端
  async handlePostMessage(
    req: IncomingMessage,
    res: ServerResponse,
    parsedBody?: unknown,
  ): Promise<void> {
    // 請(qǐng)求前置處理
    let body: string | unknown;
    try {
      const ct = contentType.parse(req.headers["content-type"] ?? "");
      if (ct.type !== "application/json") {
        throw new Error(`Unsupported content-type: ${ct}`);
      }
      body = parsedBody ?? await getRawBody(req, {
        limit: MAXIMUM_MESSAGE_SIZE,
        encoding: ct.parameters.charset ?? "utf-8",
      });
    } catch (error) {
      res.writeHead(400).end(String(error));
      this.onerror?.(error as Error);
      return;
    }
    try {
      // 具體的處理邏輯
      await this.handleMessage(typeof body === 'string' ? JSON.parse(body) : body);
    } catch {
      res.writeHead(400).end(`Invalid message: ${body}`);
      return;
    }
    // 先回復(fù) 202 Accepted,表示請(qǐng)求已經(jīng)被接受,后續(xù)處理邏輯由上層處理
    res.writeHead(202).end("Accepted");
  }
  async handleMessage(message: unknown): Promise<void> {
    let parsedMessage: JSONRPCMessage;
    // 解析請(qǐng)求,通知上層處理
    try {
      parsedMessage = JSONRPCMessageSchema.parse(message);
    } catch (error) {
      this.onerror?.(error as Error);
      throw error;
    }
    this.onmessage?.(parsedMessage);
  }
  async send(message: JSONRPCMessage): Promise<void> {
    this._sseResponse.write(
      `event: message\ndata: ${JSON.stringify(message)}\n\n`,
    );
  }
  get sessionId(): string {
    return this._sessionId;
  }
}

SSE 的處理過(guò)程如下。

  1. 客戶端發(fā)起 SSE 請(qǐng)求,服務(wù)器收到 SSE 請(qǐng)求時(shí),記錄對(duì)應(yīng)的響應(yīng)對(duì)象和設(shè)置通信路由,返回通信路由給客戶端。
  2. 客戶端收到 SSE 通信路徑后,后續(xù)通過(guò) HTTP POST 請(qǐng)求到服務(wù)端返回的路由。
  3. 服務(wù)端收到 HTTP Post 請(qǐng)求時(shí),先返回 202 表示請(qǐng)求已被成功接收,然后通知上層,上冊(cè)處理完畢后通過(guò)第一步保存的 SSE 響應(yīng)對(duì)象推送處理結(jié)果給客戶端。

接著看一下 Client 的 SSE Transport 的實(shí)現(xiàn)。

export class SSEClientTransport implements Transport {
  private _eventSource?: EventSource;
  private _endpoint?: URL;
  private _abortController?: AbortController;
  private _url: URL;
  private _eventSourceInit?: EventSourceInit;
  private _requestInit?: RequestInit;
  
  // 上層定義
  onmessage?: (message: JSONRPCMessage) => void;
  constructor(
    url: URL,
    opts?: SSEClientTransportOptions,
  ) {
    this._url = url;
    this._eventSourceInit = opts?.eventSourceInit;
    this._requestInit = opts?.requestInit;
    // 身份驗(yàn)證提供者
    this._authProvider = opts?.authProvider;
  }
  async start() {
    return await this._startOrAuth();
  }
  private _startOrAuth(): Promise<void> {
    return new Promise((resolve, reject) => {
      // SSE 客戶端
      this._eventSource = new EventSource(
        this._url.href,
        this._eventSourceInit ?? {
          // 自定義請(qǐng)求實(shí)現(xiàn)
          fetch: (url, init) => this._commonHeaders().then((headers) => fetch(url, {
            ...init,
            headers: {
              ...headers,
              Accept: "text/event-stream"
            }
          })),
        },
      );
      // 接收服務(wù)器的 endpoint,用于后續(xù)通信
      this._eventSource.addEventListener("endpoint", (event: Event) => {
        const messageEvent = event as MessageEvent;
        this._endpoint = new URL(messageEvent.data, this._url);
        resolve();
      });
      // 接收服務(wù)端消息
      this._eventSource.onmessage = (event: Event) => {
        const messageEvent = event as MessageEvent;
        let message: JSONRPCMessage;
        try {
          message = JSONRPCMessageSchema.parse(JSON.parse(messageEvent.data));
        } catch (error) {
          this.onerror?.(error as Error);
          return;
        }
        this.onmessage?.(message);
      };
    });
  }
  // 發(fā)送消息給客戶端
  async send(message: JSONRPCMessage): Promise<void> {
    const headers = new Headers({ ...this._requestInit?.headers });
    headers.set("content-type", "application/json");
    const init = {
      ...this._requestInit,
      method: "POST",
      headers,
      body: JSON.stringify(message),
      signal: this._abortController?.signal,
    };
    const response = await fetch(this._endpoint, init);
  }
}

Server

了解了 Transport 后,繼續(xù)看 Server 是如何處理來(lái)自 Transport 的請(qǐng)求的。

class Server extends Protocol {
  private _clientCapabilities?: ClientCapabilities;
  private _clientVersion?: Implementation;
  private _capabilities: ServerCapabilities;
  private _instructions?: string;
  constructor(
    private _serverInfo: Implementation,
    options?: ServerOptions,
  ) {
    super(options);
    // 支持的能力
    this._capabilities = options?.capabilities ?? {};
    this._instructions = options?.instructions;
    // 注冊(cè)初始化請(qǐng)求路由和處理函數(shù),該請(qǐng)求用戶獲取服務(wù)端的一些元信息,比如支持的能力
    this.setRequestHandler(InitializeRequestSchema, (request) =>
      this._oninitialize(request),
    );
  }
  private async _oninitialize(
    request: InitializeRequest,
  ): Promise<InitializeResult> {
    const requestedVersion = request.params.protocolVersion;
    this._clientCapabilities = request.params.capabilities;
    this._clientVersion = request.params.clientInfo;
    return {
      protocolVersion: SUPPORTED_PROTOCOL_VERSIONS.includes(requestedVersion)
        ? requestedVersion
        : LATEST_PROTOCOL_VERSION,
      capabilities: this.getCapabilities(),
      serverInfo: this._serverInfo,
      ...(this._instructions && { instructions: this._instructions }),
    };
  }
  // 注冊(cè) Server 支持的能力,比如工具、Prompt
  public registerCapabilities(capabilities: ServerCapabilities): void {
    this._capabilities = mergeCapabilities(this._capabilities, capabilities);
  }
}

Server 繼承了 Protocol,本身處理了 InitializeRequestSchema 請(qǐng)求,用于返回服務(wù)端的元信息,接著看 Protocol。

Protocol


export abstract class Protocol {
  private _transport?: Transport;
  private _requestMessageId = 0;
  private _requestHandlers = new Map();
  private _responseHandlers = new Map();
  // 初始化實(shí)例,保存 transport,后續(xù)用 transport 和對(duì)象通信
  async connect(transport: Transport): Promise<void> {
    this._transport = transport;
    this._transport.onmessage = (message) => {
      // 根據(jù) message 的類型,分別處理請(qǐng)求、響應(yīng)、通知,因?yàn)樵擃悤?huì)被 Clien 和 Server 繼承
      if (!("method" in message)) {
        this._onresponse(message);
      } else if ("id" in message) {
        this._onrequest(message);
      } else {
        this._onnotification(message);
      }
    };
    await this._transport.start();
  }
  private _onrequest(request: JSONRPCRequest): void {
    const handler = this._requestHandlers.get(request.method) ?? this.fallbackRequestHandler;
    const extra = {
      sessionId: this._transport?.sessionId,
    };
    Promise.resolve()
      .then(() => handler(request, extra)) // 處理請(qǐng)求
      .then(
        (result) => {
          // 發(fā)送處理請(qǐng)求的結(jié)果
          return this._transport?.send({
            result,
            jsonrpc: "2.0",
            id: request.id,
          });
        })
  }
  private _onresponse(response: JSONRPCResponse | JSONRPCError): void {
    const messageId = Number(response.id);
    const handler = this._responseHandlers.get(messageId);
    this._responseHandlers.delete(messageId);
    handler(response);
  }
  request<T extends ZodType<object>>(
    request: SendRequestT,
    resultSchema: T,
    options?: RequestOptions,
  ): Promise<z.infer<T>> {
    return new Promise((resolve, reject) => {
      // 請(qǐng)求 ID 遞增
      const messageId = this._requestMessageId++;
      const jsonrpcRequest: JSONRPCRequest = {
        ...request,
        jsonrpc: "2.0",
        id: messageId,
      };
      // 設(shè)置請(qǐng)求和處理函數(shù)的映射,在 _onresponse 處理響應(yīng)
      this._responseHandlers.set(messageId, (response) => {
        try {
          const result = resultSchema.parse(response.result);
          resolve(result);
        } catch (error) {
          reject(error);
        }
      });
      // 發(fā)送請(qǐng)求
      this._transport.send(jsonrpcRequest);
    });
  }
  // 注冊(cè)路由和處理函數(shù)
  setRequestHandler<
    T extends ZodObject<{
      method: ZodLiteral<string>;
    }>,
  >(
    requestSchema: T,
    handler: (
      request: z.infer<T>,
      extra: RequestHandlerExtra,
    ) => SendResultT | Promise<SendResultT>,
  ): void {
    const method = requestSchema.shape.method.value;
    // 存入 map 
    this._requestHandlers.set(method, (request, extra) =>
      Promise.resolve(handler(requestSchema.parse(request), extra)),
    );
  }
}

Protocol 基于 Transport 實(shí)現(xiàn)了對(duì)請(qǐng)求和響應(yīng)的封裝,具體請(qǐng)求和響應(yīng)的處理由上層的 Client 或 Server 執(zhí)行。

MCP Server

通過(guò)前面的分析可以知道,已經(jīng)實(shí)現(xiàn)了數(shù)據(jù)通信、路由注冊(cè)和處理,請(qǐng)求和響應(yīng)的封裝,接著看 MCP Server 的實(shí)現(xiàn),MCP Server 在之前的基礎(chǔ)上實(shí)現(xiàn)了一系列對(duì)外的 API 和提供注冊(cè)工具、Prompt 等能力。

export class McpServer {
  public readonly server: Server;
  private _registeredTools = {};
  constructor(serverInfo: Implementation, options?: ServerOptions) {
  private _registeredTools: { [name: string]: Tool } = {};
  }
  async connect(transport: Transport): Promise<void> {
    return await this.server.connect(transport);
  }
  private setToolRequestHandlers() {
    // 表示 Server 支持工具
    this.server.registerCapabilities({
      tools: {},
    });
    // 注冊(cè)獲取工具列表路由
    this.server.setRequestHandler(
      ListToolsRequestSchema,
      (): ListToolsResult => ({
        tools: Object.entries(this._registeredTools).map(
          ([name, tool]): Tool => {
            return {
              name,
              description: tool.description,
              inputSchema: tool.inputSchema
                ? (zodToJsonSchema(tool.inputSchema, {
                    strictUnions: true,
                  }) as Tool["inputSchema"])
                : EMPTY_OBJECT_JSON_SCHEMA,
            };
          },
        ),
      }),
    );
    // 注冊(cè)調(diào)用工具路由
    this.server.setRequestHandler(
      CallToolRequestSchema,
      async (request, extra): Promise<CallToolResult> => {
        // 獲取對(duì)應(yīng)的工具
        const tool = this._registeredTools[request.params.name];
        const parseResult = await tool.inputSchema.safeParseAsync(
          request.params.arguments,
        );
        // 解析參數(shù)
        const args = parseResult.data;
        const cb = tool.callback as ToolCallback<ZodRawShape>;
        return await Promise.resolve(cb(args, extra));
      },
    );
  }
  // 注冊(cè)工具,name為工具名,description為工具描述,paramsSchema為參數(shù)的zod schema,cb為工具的回調(diào)函數(shù)
  tool(name: string, ...rest: unknown[]): void {
    if (this._registeredTools[name]) {
      throw new Error(`Tool ${name} is already registered`);
    }
    let description;
    if (typeof rest[0] === "string") {
      description = rest.shift() as string;
    }
    let paramsSchema;
    if (rest.length > 1) {
      paramsSchema = rest.shift() as ZodRawShape;
    }
    const cb = rest[0];
    // 記錄工具信息
    this._registeredTools[name] = {
      description,
      inputSchema:
        paramsSchema === undefined ? undefined : z.object(paramsSchema),
      callback: cb,
    };
    // 給 MCP Server 注冊(cè)路由,這樣就可以處理客戶端的工具相關(guān)請(qǐng)求
    this.setToolRequestHandlers();
  }
}

MCP Server 主要提供了一些 API 方便用戶注冊(cè)工具、Prompt 等,如果注冊(cè)了相關(guān)的能力,那么 MCP Server 就會(huì)注冊(cè)對(duì)應(yīng)的路由,這樣客戶端就可以訪問(wèn)這些路由獲取相關(guān)能力。

Client

Client 繼承了 Protocol 并通過(guò) Transport 實(shí)現(xiàn)和 MCP Server 的通信 。

export class Client extends Protocol {
  private _serverCapabilities?: ServerCapabilities;
  private _serverVersion?: Implementation;
  private _capabilities: ClientCapabilities;
  private _instructions?: string;
  constructor(
    private _clientInfo: Implementation,
    options?: ClientOptions,
  ) {
    super(options);
    // 客戶端支持的能力
    this._capabilities = options?.capabilities ?? {};
  }
  public registerCapabilities(capabilities: ClientCapabilities): void {
    this._capabilities = mergeCapabilities(this._capabilities, capabilities);
  }
  override async connect(transport: Transport): Promise<void> {
    // 建立和服務(wù)器的連接和身份驗(yàn)證,由 Transport 實(shí)現(xiàn)提供
    await super.connect(transport);
    try {
      // 發(fā)送初始化請(qǐng)求獲取服務(wù)器元信息,比如支持的能力
      const result = await this.request(
        {
          method: "initialize",
          params: {
            protocolVersion: LATEST_PROTOCOL_VERSION,
            capabilities: this._capabilities,
            clientInfo: this._clientInfo,
          },
        },
        InitializeResultSchema,
      );
      if (result === undefined) {
        throw new Error(`Server sent invalid initialize result: ${result}`);
      }
      if (!SUPPORTED_PROTOCOL_VERSIONS.includes(result.protocolVersion)) {
        throw new Error(
          `Server's protocol version is not supported: ${result.protocolVersion}`,
        );
      }
      this._serverCapabilities = result.capabilities;
      this._serverVersion = result.serverInfo;
      this._instructions = result.instructions;
      await this.notification({
        method: "notifications/initialized",
      });
    } catch (error) {
      // Disconnect if initialization fails.
      void this.close();
      throw error;
    }
  }
  // 調(diào)用工具
  async callTool(
    params: CallToolRequest["params"],
    resultSchema,
    options?: RequestOptions,
  ) {
    return this.request(
      { method: "tools/call", params },
      resultSchema,
      options,
    );
  }
  // 獲取工具列表
  async listTools(
    params?: ListToolsRequest["params"],
    options?: RequestOptions,
  ) {
    return this.request(
      { method: "tools/list", params },
      ListToolsResultSchema,
      options,
    );
  }
}

Client 封裝了 MCP 協(xié)議定義的一系列 API,比如獲取工具、Prompt 列表,調(diào)用工具等。下面是 MCP 客戶端和服務(wù)器一次請(qǐng)求的通信過(guò)程。

MCP 在 OpenManus 中使用

下面以 OpenManus 為例,看看 MCP 是如何和大模型結(jié)合使用的。下面是 run_mcp.py 的代碼。

async def run_mcp() -> None:
    """Main entry point for the MCP runner."""
    args = parse_args()
    runner = MCPRunner()
    await runner.initialize(args.connection, args.server_url)
    await runner.run_default()
if __name__ == "__main__":
    asyncio.run(run_mcp())

上面的代碼中創(chuàng)建了一個(gè) MCPRunner 對(duì)象,然后執(zhí)行它的 initialize 和 run_default 方法,看看 MCPRunner 的實(shí)現(xiàn)。

class MCPRunner:
    def __init__(self):
        self.agent = MCPAgent()
    async def initialize(
        self,
        connection_type: str,
        server_url: str | None = None,
    ) -> None:
        // 建立和 MCP 服務(wù)器的連接
        await self.agent.initialize(connection_type="sse", server_url=server_url)
    async def run_default(self) -> None:
        # 提示用戶輸入問(wèn)題
        prompt = input("Enter your prompt: ")
        # 開(kāi)始處理用戶輸入的問(wèn)題
        await self.agent.run(prompt)

MCPRunner 是對(duì) MCPAgent 封裝,并最終調(diào)了 MCPAgent 的 initialize 和 run 方法。接著看 MCPAgent 的實(shí)現(xiàn)。

class MCPAgent(ToolCallAgent):
    mcp_clients: MCPClients = Field(default_factory=MCPClients)
    
    async def initialize(
        self,
        connection_type: Optional[str] = None,
        server_url: Optional[str] = None,
        command: Optional[str] = None,
        args: Optional[List[str]] = None,
    ) -> None:
        
        # 初始化和 MCP 服務(wù)器的連接,并獲取工具列表
        await self.mcp_clients.connect_sse(server_url=server_url)
        # 保存工具相關(guān)的實(shí)例到 available_tools
        self.available_tools = self.mcp_clients
    async def run(self, request: Optional[str] = None) -> str:
        result = await super().run(request)
        return result

MCPAgent 通過(guò) MCPClients 建立和 MCP Server 的連接,然后獲取工具列表??纯?MCP Client 的實(shí)現(xiàn)。

class MCPClients(ToolCollection):
    session: Optional[ClientSession] = None
    exit_stack: AsyncExitStack = None
    description: str = "MCP client tools for server interaction"
    def __init__(self):
        super().__init__()  # Initialize with empty tools list
        self.name = "mcp"  # Keep name for backward compatibility
        self.exit_stack = AsyncExitStack()
    async def connect_sse(self, server_url: str) -> None:
        streams_context = sse_client(url=server_url)
        streams = await self.exit_stack.enter_async_context(streams_context)
        self.session = await self.exit_stack.enter_async_context(
            ClientSession(*streams)
        )
        await self._initialize_and_list_tools()
    # 從 MCP Server 獲取工具列表
    async def _initialize_and_list_tools(self) -> None:
        
        await self.session.initialize()
        # 從 MCP Server 獲取工具列表
        response = await self.session.list_tools()
        # 存起來(lái)
        self.tools = tuple()
        self.tool_map = {}
        # Create proper tool objects for each server tool
        for tool in response.tools:
            server_tool = MCPClientTool(
                name=tool.name,
                descriptinotallow=tool.description,
                parameters=tool.inputSchema,
                sessinotallow=self.session,
            )
            self.tool_map[tool.name] = server_tool
        self.tools = tuple(self.tool_map.values())

獲取工具列表后,就執(zhí)行 MCPAgent 的 run 啟動(dòng)。

async def run(self, request: Optional[str] = None) -> str:
        
        if self.state != AgentState.IDLE:
            raise RuntimeError(f"Cannot run agent from state: {self.state}")
        if request:
            self.update_memory("user", request)
        results: List[str] = []
        # 循環(huán)調(diào)大模型
        async with self.state_context(AgentState.RUNNING):
            while (
                self.current_step < self.max_steps and self.state != AgentState.FINISHED
            ):
                self.current_step += 1
                logger.info(f"Executing step {self.current_step}/{self.max_steps}")
                # 不斷調(diào) step,子類實(shí)現(xiàn)
                step_result = await self.step()
                results.append(f"Step {self.current_step}: {step_result}")
            if self.current_step >= self.max_steps:
                self.current_step = 0
                self.state = AgentState.IDLE
                results.append(f"Terminated: Reached max steps ({self.max_steps})")
        await SANDBOX_CLIENT.cleanup()
        return "\n".join(results) if results else "No steps executed"

run 不斷調(diào) step 方法,step 方法由子類實(shí)現(xiàn)。通過(guò)繼承鏈可以看到 MCPAgent 繼承 ToolCallAgent,ToolCallAgent 繼承 ReActAgent,ReActAgent 實(shí)現(xiàn)了 step。

async def step(self) -> str:
        """Execute a single step: think and act."""
        should_act = await self.think()
        if not should_act:
            return "Thinking complete - no action needed"
        return await self.act()

step 中首先調(diào) think 然后再調(diào) act。這兩個(gè)方法由 ToolCallAgent 實(shí)現(xiàn)。

async def think(self) -> bool:
        
        try:
            # 傳入工具調(diào)大模型,詢問(wèn)需要執(zhí)行的工具
            response = await self.llm.ask_tool(
                messages=self.messages,
                # 傳入可用的工具
                tools=self.available_tools.to_params(),
                tool_choice=self.tool_choices,
            )
        
        self.tool_calls = tool_calls = (
            response.tool_calls if response and response.tool_calls else []
        )
    async def act(self) -> str:
        results = []
        # 執(zhí)行模型返回的工具列表
        for command in self.tool_calls:
            result = await self.execute_tool(command)
            results.append(result)
        return "\n\n".join(results)
    # 執(zhí)行某個(gè)工具
    async def execute_tool(self, command: ToolCall) -> str:
       
        name = command.function.name
        if name not in self.available_tools.tool_map:
            return f"Error: Unknown tool '{name}'"
        try:
            # 解析大模型提取的參數(shù)
            args = json.loads(command.function.arguments or "{}")
            # 執(zhí)行工具
            result = await self.available_tools.execute(name=name, tool_input=args)
            # Format result for display (standard case)
            observation = (
                f"Observed output of cmd `{name}` executed:\n{str(result)}"
                if result
                else f"Cmd `{name}` completed with no output"
            )
            return observation

這里以 OpenManus 提供的 Bash 工具為例。

class _BashSession:
    command: str = "/bin/bash"
    async def start(self):
        # 創(chuàng)建一個(gè) bash 進(jìn)程
        self._process = await asyncio.create_subprocess_shell(
            self.command,
            preexec_fn=os.setsid,
            shell=True,
            bufsize=0,
            stdin=asyncio.subprocess.PIPE,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE,
        )
    async def run(self, command: str):
        # 讓 bash 執(zhí)行命令
        self._process.stdin.write(
            command.encode() + f"; echo '{self._sentinel}'\n".encode()
        )
        await self._process.stdin.drain()
        # 獲取輸出
class Bash(BaseTool):
    """A tool for executing bash commands"""
    name: str = "bash"
    description: str = _BASH_DESCRIPTION
    parameters: dict = {
        "type": "object",
        "properties": {
            "command": {
                "type": "string",
                "description": "The bash command to execute. Can be empty to view additional logs when previous exit code is `-1`. Can be `ctrl+c` to interrupt the currently running process.",
            },
        },
        "required": ["command"],
    }
    _session: Optional[_BashSession] = None
    async def execute(
        self, command: str | None = None, restart: bool = False, **kwargs
    ) -> CLIResult:
        if restart:
            if self._session:
                self._session.stop()
            self._session = _BashSession()
            await self._session.start()
            return CLIResult(system="tool has been restarted.")
        if self._session is None:
            self._session = _BashSession()
            await self._session.start()
        if command is not None:
            return await self._session.run(command)
        raise ToolError("no command provided.")

Bash 工具或創(chuàng)建一個(gè)子進(jìn)程并執(zhí)行大模型從用戶輸入中提取的命令。從代碼來(lái)看 OpenManus 只是收集并展示工具的輸出,而沒(méi)有再次把工具的輸出傳給模型進(jìn)行下一步的查詢。


參考資料:

https://github.com/mannaandpoem/OpenManus/tree/main。

https://github.com/modelcontextprotocol/typescript-sdk。

https://github.com/modelcontextprotocol/servers。

Introduction - Model Context Protocol。

https://onevcat.com/2025/02/mcp/。

責(zé)任編輯:姜華 來(lái)源: 編程雜技
相關(guān)推薦

2021-03-26 22:23:13

Python算法底層

2020-12-17 08:03:57

LinkedList面試源碼

2020-12-14 08:03:52

ArrayList面試源碼

2021-03-16 21:45:59

Python Resize機(jī)制

2017-04-05 20:00:32

ChromeObjectJS代碼

2020-08-26 14:00:37

C++string語(yǔ)言

2024-12-23 14:12:41

2025-03-12 00:45:25

MCPJavaSDK

2025-04-01 08:45:56

2025-04-17 08:42:38

2025-04-02 10:06:00

2011-06-08 09:22:54

Samba

2023-12-25 11:18:12

OpenTeleme應(yīng)用日志Loki

2021-08-12 07:01:23

FlutterRouter Android

2020-10-09 14:13:04

Zookeeper Z

2021-04-15 09:07:52

hotspotJavaC++

2024-04-01 00:07:20

LinuxeBPF源碼

2018-10-31 15:54:47

Java線程池源碼

2023-12-04 07:31:41

Golangwebsocket

2023-08-11 08:42:49

泛型工廠繼承配置
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)