Skip to content

Latest commit

 

History

History
1424 lines (1097 loc) · 44.7 KB

File metadata and controls

1424 lines (1097 loc) · 44.7 KB

建立具有 LLM 的客戶端

到目前為止,您已了解如何建立伺服器和客戶端。客戶端可以明確地呼叫伺服器以列出其工具、資源和提示。然而,這種方法並不太實用。您的使用者生活在代理時代,期望使用提示並與 LLM 進行交流。對於您的使用者來說,他們不在乎您是否使用 MCP 來存儲功能,但他們期望能夠使用自然語言進行互動。那麼我們該如何解決這個問題呢?解決方案是將 LLM 添加到客戶端中。

概述

在本課程中,我們將重點放在如何將 LLM 添加到客戶端中,並展示這如何為使用者提供更好的體驗。

學習目標

完成本課程後,您將能夠:

  • 建立具有 LLM 的客戶端。
  • 使用 LLM 無縫地與 MCP 伺服器互動。
  • 在客戶端端提供更好的最終使用者體驗。

方法

讓我們嘗試了解需要採取的方法。添加 LLM 聽起來很簡單,但我們實際上會怎麼做呢?

以下是客戶端與伺服器互動的方式:

  1. 與伺服器建立連接。

  2. 列出功能、提示、資源和工具,並保存其架構。

  3. 添加 LLM,並以 LLM 能理解的格式傳遞保存的功能及其架構。

  4. 處理使用者提示,將其與客戶端列出的工具一起傳遞給 LLM。

很好,現在我們已經了解如何在高層次上完成這些操作,接下來讓我們在下面的練習中試試看。

練習:建立具有 LLM 的客戶端

在本練習中,我們將學習如何將 LLM 添加到客戶端中。

使用 GitHub 個人訪問令牌進行身份驗證

建立 GitHub 令牌是一個簡單的過程。以下是操作步驟:

  • 前往 GitHub 設定 – 點擊右上角的個人資料圖片並選擇設定。
  • 導航至開發者設定 – 向下滾動並點擊開發者設定。
  • 選擇個人訪問令牌 – 點擊個人訪問令牌,然後生成新令牌。
  • 配置您的令牌 – 添加備註以供參考,設置到期日期,並選擇必要的範圍(權限)。
  • 生成並複製令牌 – 點擊生成令牌,並確保立即複製,因為您之後將無法再次查看。

-1- 連接到伺服器

首先讓我們建立客戶端:

TypeScript

import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
import OpenAI from "openai";
import { z } from "zod"; // Import zod for schema validation

class MCPClient {
    private openai: OpenAI;
    private client: Client;
    constructor(){
        this.openai = new OpenAI({
            baseURL: "https://models.inference.ai.azure.com", 
            apiKey: process.env.GITHUB_TOKEN,
        });

        this.client = new Client(
            {
                name: "example-client",
                version: "1.0.0"
            },
            {
                capabilities: {
                prompts: {},
                resources: {},
                tools: {}
                }
            }
            );    
    }
}

在上述程式碼中,我們:

  • 引入了所需的庫。
  • 建立了一個具有兩個成員的類別,clientopenai,分別幫助我們管理客戶端和與 LLM 互動。
  • 配置了 LLM 實例以使用 GitHub 模型,通過設置 baseUrl 指向推理 API。

Python

from mcp import ClientSession, StdioServerParameters, types
from mcp.client.stdio import stdio_client

# Create server parameters for stdio connection
server_params = StdioServerParameters(
    command="mcp",  # Executable
    args=["run", "server.py"],  # Optional command line arguments
    env=None,  # Optional environment variables
)


async def run():
    async with stdio_client(server_params) as (read, write):
        async with ClientSession(
            read, write
        ) as session:
            # Initialize the connection
            await session.initialize()


if __name__ == "__main__":
    import asyncio

    asyncio.run(run())

在上述程式碼中,我們:

  • 引入了 MCP 所需的庫。
  • 建立了一個客戶端。

.NET

using Azure;
using Azure.AI.Inference;
using Azure.Identity;
using System.Text.Json;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol.Transport;
using System.Text.Json;

var clientTransport = new StdioClientTransport(new()
{
    Name = "Demo Server",
    Command = "/workspaces/mcp-for-beginners/03-GettingStarted/02-client/solution/server/bin/Debug/net8.0/server",
    Arguments = [],
});

await using var mcpClient = await McpClientFactory.CreateAsync(clientTransport);

Java

首先,您需要將 LangChain4j 依賴項添加到您的 pom.xml 文件中。添加這些依賴項以啟用 MCP 集成和 GitHub 模型支持:

<properties>
    <langchain4j.version>1.0.0-beta3</langchain4j.version>
</properties>

<dependencies>
    <!-- LangChain4j MCP Integration -->
    <dependency>
        <groupId>dev.langchain4j</groupId>
        <artifactId>langchain4j-mcp</artifactId>
        <version>${langchain4j.version}</version>
    </dependency>
    
    <!-- OpenAI Official API Client -->
    <dependency>
        <groupId>dev.langchain4j</groupId>
        <artifactId>langchain4j-open-ai-official</artifactId>
        <version>${langchain4j.version}</version>
    </dependency>
    
    <!-- GitHub Models Support -->
    <dependency>
        <groupId>dev.langchain4j</groupId>
        <artifactId>langchain4j-github-models</artifactId>
        <version>${langchain4j.version}</version>
    </dependency>
    
    <!-- Spring Boot Starter (optional, for production apps) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

然後建立您的 Java 客戶端類別:

import dev.langchain4j.mcp.McpToolProvider;
import dev.langchain4j.mcp.client.DefaultMcpClient;
import dev.langchain4j.mcp.client.McpClient;
import dev.langchain4j.mcp.client.transport.McpTransport;
import dev.langchain4j.mcp.client.transport.http.HttpMcpTransport;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openaiofficial.OpenAiOfficialChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.tool.ToolProvider;

import java.time.Duration;
import java.util.List;

public class LangChain4jClient {
    
    public static void main(String[] args) throws Exception {        // Configure the LLM to use GitHub Models
        ChatLanguageModel model = OpenAiOfficialChatModel.builder()
                .isGitHubModels(true)
                .apiKey(System.getenv("GITHUB_TOKEN"))
                .timeout(Duration.ofSeconds(60))
                .modelName("gpt-4.1-nano")
                .build();

        // Create MCP transport for connecting to server
        McpTransport transport = new HttpMcpTransport.Builder()
                .sseUrl("http://localhost:8080/sse")
                .timeout(Duration.ofSeconds(60))
                .logRequests(true)
                .logResponses(true)
                .build();

        // Create MCP client
        McpClient mcpClient = new DefaultMcpClient.Builder()
                .transport(transport)
                .build();
    }
}

在上述程式碼中,我們:

  • 添加了 LangChain4j 依賴項:需要用於 MCP 集成、OpenAI 官方客戶端和 GitHub 模型支持。
  • 引入了 LangChain4j 庫:用於 MCP 集成和 OpenAI 聊天模型功能。
  • 建立了一個 ChatLanguageModel:配置為使用 GitHub 模型並使用您的 GitHub 令牌。
  • 設置了 HTTP 傳輸:使用伺服器推送事件(SSE)連接到 MCP 伺服器。
  • 建立了一個 MCP 客戶端:負責與伺服器通信。
  • 使用 LangChain4j 的內建 MCP 支持:簡化了 LLM 和 MCP 伺服器之間的集成。

Rust

此示例假設您有一個基於 Rust 的 MCP 伺服器正在運行。如果您沒有,請參考 01-first-server 課程以建立伺服器。

一旦您有了 Rust MCP 伺服器,打開終端並導航到與伺服器相同的目錄。然後運行以下命令以建立新的 LLM 客戶端專案:

mkdir calculator-llmclient
cd calculator-llmclient
cargo init

將以下依賴項添加到您的 Cargo.toml 文件中:

[dependencies]
async-openai = { version = "0.29.0", features = ["byot"] }
rmcp = { version = "0.5.0", features = ["client", "transport-child-process"] }
serde_json = "1.0.141"
tokio = { version = "1.46.1", features = ["rt-multi-thread"] }

Note

Rust 沒有官方的 OpenAI 庫,但 async-openai crate 是一個 社群維護的庫,通常被使用。

打開 src/main.rs 文件並用以下程式碼替換其內容:

use async_openai::{Client, config::OpenAIConfig};
use rmcp::{
    RmcpError,
    model::{CallToolRequestParam, ListToolsResult},
    service::{RoleClient, RunningService, ServiceExt},
    transport::{ConfigureCommandExt, TokioChildProcess},
};
use serde_json::{Value, json};
use std::error::Error;
use tokio::process::Command;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // Initial message
    let mut messages = vec![json!({"role": "user", "content": "What is the sum of 3 and 2?"})];

    // Setup OpenAI client
    let api_key = std::env::var("OPENAI_API_KEY")?;
    let openai_client = Client::with_config(
        OpenAIConfig::new()
            .with_api_base("https://models.github.ai/inference/chat")
            .with_api_key(api_key),
    );

    // Setup MCP client
    let server_dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .unwrap()
        .join("calculator-server");

    let mcp_client = ()
        .serve(
            TokioChildProcess::new(Command::new("cargo").configure(|cmd| {
                cmd.arg("run").current_dir(server_dir);
            }))
            .map_err(RmcpError::transport_creation::<TokioChildProcess>)?,
        )
        .await?;

    // TODO: Get MCP tool listing 

    // TODO: LLM conversation with tool calls

    Ok(())
}

此程式碼設置了一個基本的 Rust 應用程式,將連接到 MCP 伺服器和 GitHub 模型以進行 LLM 互動。

Important

在運行應用程式之前,請確保使用您的 GitHub 令牌設置 OPENAI_API_KEY 環境變數。

很好,接下來的步驟是列出伺服器上的功能。

-2- 列出伺服器功能

現在我們將連接到伺服器並請求其功能:

TypeScript

在同一類別中,添加以下方法:

async connectToServer(transport: Transport) {
     await this.client.connect(transport);
     this.run();
     console.error("MCPClient started on stdin/stdout");
}

async run() {
    console.log("Asking server for available tools");

    // listing tools
    const toolsResult = await this.client.listTools();
}

在上述程式碼中,我們:

  • 添加了連接伺服器的程式碼,connectToServer
  • 建立了一個 run 方法,負責處理應用程式流程。目前它僅列出工具,但我們很快會添加更多內容。

Python

# List available resources
resources = await session.list_resources()
print("LISTING RESOURCES")
for resource in resources:
    print("Resource: ", resource)

# List available tools
tools = await session.list_tools()
print("LISTING TOOLS")
for tool in tools.tools:
    print("Tool: ", tool.name)
    print("Tool", tool.inputSchema["properties"])

以下是我們添加的內容:

  • 列出資源和工具並打印它們。對於工具,我們還列出了 inputSchema,稍後會使用它。

.NET

async Task<List<ChatCompletionsToolDefinition>> GetMcpTools()
{
    Console.WriteLine("Listing tools");
    var tools = await mcpClient.ListToolsAsync();

    List<ChatCompletionsToolDefinition> toolDefinitions = new List<ChatCompletionsToolDefinition>();

    foreach (var tool in tools)
    {
        Console.WriteLine($"Connected to server with tools: {tool.Name}");
        Console.WriteLine($"Tool description: {tool.Description}");
        Console.WriteLine($"Tool parameters: {tool.JsonSchema}");

        // TODO: convert tool definition from MCP tool to LLm tool     
    }

    return toolDefinitions;
}

在上述程式碼中,我們:

  • 列出了 MCP 伺服器上可用的工具。
  • 對於每個工具,列出了名稱、描述及其架構。後者是我們稍後用於呼叫工具的內容。

Java

// Create a tool provider that automatically discovers MCP tools
ToolProvider toolProvider = McpToolProvider.builder()
        .mcpClients(List.of(mcpClient))
        .build();

// The MCP tool provider automatically handles:
// - Listing available tools from the MCP server
// - Converting MCP tool schemas to LangChain4j format
// - Managing tool execution and responses

在上述程式碼中,我們:

  • 建立了一個 McpToolProvider,自動發現並註冊 MCP 伺服器中的所有工具。
  • 工具提供者在 MCP 工具架構和 LangChain4j 工具格式之間進行內部轉換。
  • 此方法抽象了手動工具列出和轉換過程。

Rust

從 MCP 伺服器檢索工具是通過 list_tools 方法完成的。在您的 main 函數中,設置 MCP 客戶端後,添加以下程式碼:

// Get MCP tool listing 
let tools = mcp_client.list_tools(Default::default()).await?;

-3- 將伺服器功能轉換為 LLM 工具

列出伺服器功能後的下一步是將其轉換為 LLM 能理解的格式。一旦完成,我們就可以將這些功能作為工具提供給 LLM。

TypeScript

  1. 添加以下程式碼以將 MCP 伺服器的響應轉換為 LLM 可用的工具格式:

    openAiToolAdapter(tool: {
        name: string;
        description?: string;
        input_schema: any;
        }) {
        // Create a zod schema based on the input_schema
        const schema = z.object(tool.input_schema);
    
        return {
            type: "function" as const, // Explicitly set type to "function"
            function: {
            name: tool.name,
            description: tool.description,
            parameters: {
            type: "object",
            properties: tool.input_schema.properties,
            required: tool.input_schema.required,
            },
            },
        };
    }

    上述程式碼將 MCP 伺服器的響應轉換為 LLM 可理解的工具定義格式。

  2. 接下來更新 run 方法以列出伺服器功能:

    async run() {
        console.log("Asking server for available tools");
        const toolsResult = await this.client.listTools();
        const tools = toolsResult.tools.map((tool) => {
            return this.openAiToolAdapter({
            name: tool.name,
            description: tool.description,
            input_schema: tool.inputSchema,
            });
        });
    }

    在上述程式碼中,我們更新了 run 方法,對結果進行映射,並對每個條目呼叫 openAiToolAdapter

Python

  1. 首先,建立以下轉換函數:

    def convert_to_llm_tool(tool):
        tool_schema = {
            "type": "function",
            "function": {
                "name": tool.name,
                "description": tool.description,
                "type": "function",
                "parameters": {
                    "type": "object",
                    "properties": tool.inputSchema["properties"]
                }
            }
        }
    
        return tool_schema

    在上述 convert_to_llm_tools 函數中,我們將 MCP 工具響應轉換為 LLM 可理解的格式。

  2. 接下來,更新客戶端程式碼以利用此函數:

    for tool in tools.tools:
        print("Tool: ", tool.name)
        print("Tool", tool.inputSchema["properties"])
        functions.append(convert_to_llm_tool(tool))

    在此,我們添加了對 convert_to_llm_tool 的呼叫,以將 MCP 工具響應轉換為稍後可提供給 LLM 的內容。

.NET

  1. 添加程式碼以將 MCP 工具響應轉換為 LLM 可理解的內容:
ChatCompletionsToolDefinition ConvertFrom(string name, string description, JsonElement jsonElement)
{ 
    // convert the tool to a function definition
    FunctionDefinition functionDefinition = new FunctionDefinition(name)
    {
        Description = description,
        Parameters = BinaryData.FromObjectAsJson(new
        {
            Type = "object",
            Properties = jsonElement
        },
        new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase })
    };

    // create a tool definition
    ChatCompletionsToolDefinition toolDefinition = new ChatCompletionsToolDefinition(functionDefinition);
    return toolDefinition;
}

在上述程式碼中,我們:

  • 建立了一個函數 ConvertFrom,接受名稱、描述和輸入架構。
  • 定義了功能,建立一個 FunctionDefinition,並將其傳遞給 ChatCompletionsDefinition。後者是 LLM 可理解的內容。
  1. 更新現有程式碼以利用上述函數:

    async Task<List<ChatCompletionsToolDefinition>> GetMcpTools()
    {
        Console.WriteLine("Listing tools");
        var tools = await mcpClient.ListToolsAsync();
    
        List<ChatCompletionsToolDefinition> toolDefinitions = new List<ChatCompletionsToolDefinition>();
    
        foreach (var tool in tools)
        {
            Console.WriteLine($"Connected to server with tools: {tool.Name}");
            Console.WriteLine($"Tool description: {tool.Description}");
            Console.WriteLine($"Tool parameters: {tool.JsonSchema}");
    
            JsonElement propertiesElement;
            tool.JsonSchema.TryGetProperty("properties", out propertiesElement);
    
            var def = ConvertFrom(tool.Name, tool.Description, propertiesElement);
            Console.WriteLine($"Tool definition: {def}");
            toolDefinitions.Add(def);
    
            Console.WriteLine($"Properties: {propertiesElement}");        
        }
    
        return toolDefinitions;
    }

    在上述程式碼中,我們:

    • 更新函數以將 MCP 工具響應轉換為 LLM 工具。以下是我們添加的程式碼:

      JsonElement propertiesElement;
      tool.JsonSchema.TryGetProperty("properties", out propertiesElement);
      
      var def = ConvertFrom(tool.Name, tool.Description, propertiesElement);
      Console.WriteLine($"Tool definition: {def}");
      toolDefinitions.Add(def);

      輸入架構是工具響應的一部分,但位於 "properties" 屬性中,因此我們需要提取。此外,我們現在使用工具詳細資訊呼叫 ConvertFrom。現在我們已完成繁重的工作,接下來讓我們看看如何處理使用者提示。

Java

// Create a Bot interface for natural language interaction
public interface Bot {
    String chat(String prompt);
}

// Configure the AI service with LLM and MCP tools
Bot bot = AiServices.builder(Bot.class)
        .chatLanguageModel(model)
        .toolProvider(toolProvider)
        .build();

在上述程式碼中,我們:

  • 定義了一個簡單的 Bot 介面,用於自然語言互動。
  • 使用 LangChain4j 的 AiServices 自動綁定 LLM 和 MCP 工具提供者。
  • 框架自動處理工具架構轉換和函數呼叫。
  • 此方法消除了手動工具轉換的需求 - LangChain4j 處理 MCP 工具到 LLM 兼容格式的所有複雜性。

Rust

要將 MCP 工具響應轉換為 LLM 可理解的格式,我們將添加一個輔助函數,用於格式化工具列表。在 main.rs 文件中,將此程式碼添加到 main 函數下方。這將在向 LLM 發送請求時被呼叫:

async fn format_tools(tools: &ListToolsResult) -> Result<Vec<Value>, Box<dyn Error>> {
    let tools_json = serde_json::to_value(tools)?;
    let Some(tools_array) = tools_json.get("tools").and_then(|t| t.as_array()) else {
        return Ok(vec![]);
    };

    let formatted_tools = tools_array
        .iter()
        .filter_map(|tool| {
            let name = tool.get("name")?.as_str()?;
            let description = tool.get("description")?.as_str()?;
            let schema = tool.get("inputSchema")?;

            Some(json!({
                "type": "function",
                "function": {
                    "name": name,
                    "description": description,
                    "parameters": {
                        "type": "object",
                        "properties": schema.get("properties").unwrap_or(&json!({})),
                        "required": schema.get("required").unwrap_or(&json!([]))
                    }
                }
            }))
        })
        .collect();

    Ok(formatted_tools)
}

很好,我們現在已準備好處理任何使用者請求,接下來讓我們解決這個問題。

-4- 處理使用者提示請求

在此部分程式碼中,我們將處理使用者請求。

TypeScript

  1. 添加一個方法,用於呼叫 LLM:

    async callTools(
        tool_calls: OpenAI.Chat.Completions.ChatCompletionMessageToolCall[],
        toolResults: any[]
    ) {
        for (const tool_call of tool_calls) {
        const toolName = tool_call.function.name;
        const args = tool_call.function.arguments;
    
        console.log(`Calling tool ${toolName} with args ${JSON.stringify(args)}`);
    
    
        // 2. Call the server's tool 
        const toolResult = await this.client.callTool({
            name: toolName,
            arguments: JSON.parse(args),
        });
    
        console.log("Tool result: ", toolResult);
    
        // 3. Do something with the result
        // TODO  
    
        }
    }

    在上述程式碼中,我們:

    • 添加了一個方法 callTools

    • 該方法接受 LLM 響應並檢查是否有工具被呼叫:

      for (const tool_call of tool_calls) {
      const toolName = tool_call.function.name;
      const args = tool_call.function.arguments;
      
      console.log(`Calling tool ${toolName} with args ${JSON.stringify(args)}`);
      
      // call tool
      }
    • 如果 LLM 指示應呼叫工具,則呼叫工具:

      // 2. Call the server's tool 
      const toolResult = await this.client.callTool({
          name: toolName,
          arguments: JSON.parse(args),
      });
      
      console.log("Tool result: ", toolResult);
      
      // 3. Do something with the result
      // TODO  
  2. 更新 run 方法以包含對 LLM 的呼叫以及呼叫 callTools

    // 1. Create messages that's input for the LLM
    const prompt = "What is the sum of 2 and 3?"
    
    const messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = [
            {
                role: "user",
                content: prompt,
            },
        ];
    
    console.log("Querying LLM: ", messages[0].content);
    
    // 2. Calling the LLM
    let response = this.openai.chat.completions.create({
        model: "gpt-4o-mini",
        max_tokens: 1000,
        messages,
        tools: tools,
    });    
    
    let results: any[] = [];
    
    // 3. Go through the LLM response,for each choice, check if it has tool calls 
    (await response).choices.map(async (choice: { message: any; }) => {
        const message = choice.message;
        if (message.tool_calls) {
            console.log("Making tool call")
            await this.callTools(message.tool_calls, results);
        }
    });

很好,以下是完整程式碼:

import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
import OpenAI from "openai";
import { z } from "zod"; // Import zod for schema validation

class MyClient {
    private openai: OpenAI;
    private client: Client;
    constructor(){
        this.openai = new OpenAI({
            baseURL: "https://models.inference.ai.azure.com", // might need to change to this url in the future: https://models.github.ai/inference
            apiKey: process.env.GITHUB_TOKEN,
        });

        this.client = new Client(
            {
                name: "example-client",
                version: "1.0.0"
            },
            {
                capabilities: {
                prompts: {},
                resources: {},
                tools: {}
                }
            }
            );    
    }

    async connectToServer(transport: Transport) {
        await this.client.connect(transport);
        this.run();
        console.error("MCPClient started on stdin/stdout");
    }

    openAiToolAdapter(tool: {
        name: string;
        description?: string;
        input_schema: any;
          }) {
          // Create a zod schema based on the input_schema
          const schema = z.object(tool.input_schema);
      
          return {
            type: "function" as const, // Explicitly set type to "function"
            function: {
              name: tool.name,
              description: tool.description,
              parameters: {
              type: "object",
              properties: tool.input_schema.properties,
              required: tool.input_schema.required,
              },
            },
          };
    }
    
    async callTools(
        tool_calls: OpenAI.Chat.Completions.ChatCompletionMessageToolCall[],
        toolResults: any[]
      ) {
        for (const tool_call of tool_calls) {
          const toolName = tool_call.function.name;
          const args = tool_call.function.arguments;
    
          console.log(`Calling tool ${toolName} with args ${JSON.stringify(args)}`);
    
    
          // 2. Call the server's tool 
          const toolResult = await this.client.callTool({
            name: toolName,
            arguments: JSON.parse(args),
          });
    
          console.log("Tool result: ", toolResult);
    
          // 3. Do something with the result
          // TODO  
    
         }
    }

    async run() {
        console.log("Asking server for available tools");
        const toolsResult = await this.client.listTools();
        const tools = toolsResult.tools.map((tool) => {
            return this.openAiToolAdapter({
              name: tool.name,
              description: tool.description,
              input_schema: tool.inputSchema,
            });
        });

        const prompt = "What is the sum of 2 and 3?";
    
        const messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = [
            {
                role: "user",
                content: prompt,
            },
        ];

        console.log("Querying LLM: ", messages[0].content);
        let response = this.openai.chat.completions.create({
            model: "gpt-4o-mini",
            max_tokens: 1000,
            messages,
            tools: tools,
        });    

        let results: any[] = [];
    
        // 1. Go through the LLM response,for each choice, check if it has tool calls 
        (await response).choices.map(async (choice: { message: any; }) => {
          const message = choice.message;
          if (message.tool_calls) {
              console.log("Making tool call")
              await this.callTools(message.tool_calls, results);
          }
        });
    }
    
}

let client = new MyClient();
 const transport = new StdioClientTransport({
            command: "node",
            args: ["./build/index.js"]
        });

client.connectToServer(transport);

Python

  1. 添加一些需要用於呼叫 LLM 的導入:

    # llm
    import os
    from azure.ai.inference import ChatCompletionsClient
    from azure.ai.inference.models import SystemMessage, UserMessage
    from azure.core.credentials import AzureKeyCredential
    import json
  2. 接下來,添加呼叫 LLM 的函數:

    # llm
    
    def call_llm(prompt, functions):
        token = os.environ["GITHUB_TOKEN"]
        endpoint = "https://models.inference.ai.azure.com"
    
        model_name = "gpt-4o"
    
        client = ChatCompletionsClient(
            endpoint=endpoint,
            credential=AzureKeyCredential(token),
        )
    
        print("CALLING LLM")
        response = client.complete(
            messages=[
                {
                "role": "system",
                "content": "You are a helpful assistant.",
                },
                {
                "role": "user",
                "content": prompt,
                },
            ],
            model=model_name,
            tools = functions,
            # Optional parameters
            temperature=1.,
            max_tokens=1000,
            top_p=1.    
        )
    
        response_message = response.choices[0].message
        
        functions_to_call = []
    
        if response_message.tool_calls:
            for tool_call in response_message.tool_calls:
                print("TOOL: ", tool_call)
                name = tool_call.function.name
                args = json.loads(tool_call.function.arguments)
                functions_to_call.append({ "name": name, "args": args })
    
        return functions_to_call

    在上述程式碼中,我們:

    • 將我們在 MCP 伺服器上找到並轉換的函數傳遞給 LLM。
    • 然後使用這些函數呼叫 LLM。
    • 接著檢查結果以查看是否需要呼叫任何函數。
    • 最後,我們傳遞一個函數數組以進行呼叫。
  3. 最後一步,更新主程式碼:

    prompt = "Add 2 to 20"
    
    # ask LLM what tools to all, if any
    functions_to_call = call_llm(prompt, functions)
    
    # call suggested functions
    for f in functions_to_call:
        result = await session.call_tool(f["name"], arguments=f["args"])
        print("TOOLS result: ", result.content)

    在上述程式碼中,我們:

    • 通過 call_tool 使用 LLM 認為應呼叫的函數呼叫 MCP 工具。
    • 打印 MCP 伺服器工具呼叫的結果。

.NET

  1. 顯示一些用於進行 LLM 提示請求的程式碼:

    var tools = await GetMcpTools();
    
    for (int i = 0; i < tools.Count; i++)
    {
        var tool = tools[i];
        Console.WriteLine($"MCP Tools def: {i}: {tool}");
    }
    
    // 0. Define the chat history and the user message
    var userMessage = "add 2 and 4";
    
    chatHistory.Add(new ChatRequestUserMessage(userMessage));
    
    // 1. Define tools
    ChatCompletionsToolDefinition def = CreateToolDefinition();
    
    
    // 2. Define options, including the tools
    var options = new ChatCompletionsOptions(chatHistory)
    {
        Model = "gpt-4o-mini",
        Tools = { tools[0] }
    };
    
    // 3. Call the model  
    
    ChatCompletions? response = await client.CompleteAsync(options);
    var content = response.Content;

    在上述程式碼中,我們:

    • 從 MCP 伺服器獲取工具,var tools = await GetMcpTools()
    • 定義了一個使用者提示 userMessage
    • 建立了一個選項物件,指定模型和工具。
    • 向 LLM 發送了一個請求。
  2. 最後一步,查看 LLM 是否認為應呼叫某個函數:

    // 4. Check if the response contains a function call
    ChatCompletionsToolCall? calls = response.ToolCalls.FirstOrDefault();
    for (int i = 0; i < response.ToolCalls.Count; i++)
    {
        var call = response.ToolCalls[i];
        Console.WriteLine($"Tool call {i}: {call.Name} with arguments {call.Arguments}");
        //Tool call 0: add with arguments {"a":2,"b":4}
    
        var dict = JsonSerializer.Deserialize<Dictionary<string, object>>(call.Arguments);
        var result = await mcpClient.CallToolAsync(
            call.Name,
            dict!,
            cancellationToken: CancellationToken.None
        );
    
        Console.WriteLine(result.Content.First(c => c.Type == "text").Text);
    
    }

    在上述程式碼中,我們:

    • 遍歷函數呼叫列表。
    • 對於每個工具呼叫,解析名稱和參數,並使用 MCP 客戶端呼叫 MCP 伺服器上的工具。最後打印結果。

以下是完整程式碼:

using Azure;
using Azure.AI.Inference;
using Azure.Identity;
using System.Text.Json;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol.Transport;
using System.Text.Json;

var endpoint = "https://models.inference.ai.azure.com";
var token = Environment.GetEnvironmentVariable("GITHUB_TOKEN"); // Your GitHub Access Token
var client = new ChatCompletionsClient(new Uri(endpoint), new AzureKeyCredential(token));
var chatHistory = new List<ChatRequestMessage>
{
    new ChatRequestSystemMessage("You are a helpful assistant that knows about AI")
};

var clientTransport = new StdioClientTransport(new()
{
    Name = "Demo Server",
    Command = "/workspaces/mcp-for-beginners/03-GettingStarted/02-client/solution/server/bin/Debug/net8.0/server",
    Arguments = [],
});

Console.WriteLine("Setting up stdio transport");

await using var mcpClient = await McpClientFactory.CreateAsync(clientTransport);

ChatCompletionsToolDefinition ConvertFrom(string name, string description, JsonElement jsonElement)
{ 
    // convert the tool to a function definition
    FunctionDefinition functionDefinition = new FunctionDefinition(name)
    {
        Description = description,
        Parameters = BinaryData.FromObjectAsJson(new
        {
            Type = "object",
            Properties = jsonElement
        },
        new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase })
    };

    // create a tool definition
    ChatCompletionsToolDefinition toolDefinition = new ChatCompletionsToolDefinition(functionDefinition);
    return toolDefinition;
}



async Task<List<ChatCompletionsToolDefinition>> GetMcpTools()
{
    Console.WriteLine("Listing tools");
    var tools = await mcpClient.ListToolsAsync();

    List<ChatCompletionsToolDefinition> toolDefinitions = new List<ChatCompletionsToolDefinition>();

    foreach (var tool in tools)
    {
        Console.WriteLine($"Connected to server with tools: {tool.Name}");
        Console.WriteLine($"Tool description: {tool.Description}");
        Console.WriteLine($"Tool parameters: {tool.JsonSchema}");

        JsonElement propertiesElement;
        tool.JsonSchema.TryGetProperty("properties", out propertiesElement);

        var def = ConvertFrom(tool.Name, tool.Description, propertiesElement);
        Console.WriteLine($"Tool definition: {def}");
        toolDefinitions.Add(def);

        Console.WriteLine($"Properties: {propertiesElement}");        
    }

    return toolDefinitions;
}

// 1. List tools on mcp server

var tools = await GetMcpTools();
for (int i = 0; i < tools.Count; i++)
{
    var tool = tools[i];
    Console.WriteLine($"MCP Tools def: {i}: {tool}");
}

// 2. Define the chat history and the user message
var userMessage = "add 2 and 4";

chatHistory.Add(new ChatRequestUserMessage(userMessage));


// 3. Define options, including the tools
var options = new ChatCompletionsOptions(chatHistory)
{
    Model = "gpt-4o-mini",
    Tools = { tools[0] }
};

// 4. Call the model  

ChatCompletions? response = await client.CompleteAsync(options);
var content = response.Content;

// 5. Check if the response contains a function call
ChatCompletionsToolCall? calls = response.ToolCalls.FirstOrDefault();
for (int i = 0; i < response.ToolCalls.Count; i++)
{
    var call = response.ToolCalls[i];
    Console.WriteLine($"Tool call {i}: {call.Name} with arguments {call.Arguments}");
    //Tool call 0: add with arguments {"a":2,"b":4}

    var dict = JsonSerializer.Deserialize<Dictionary<string, object>>(call.Arguments);
    var result = await mcpClient.CallToolAsync(
        call.Name,
        dict!,
        cancellationToken: CancellationToken.None
    );

    Console.WriteLine(result.Content.First(c => c.Type == "text").Text);

}

// 5. Print the generic response
Console.WriteLine($"Assistant response: {content}");

Java

try {
    // Execute natural language requests that automatically use MCP tools
    String response = bot.chat("Calculate the sum of 24.5 and 17.3 using the calculator service");
    System.out.println(response);

    response = bot.chat("What's the square root of 144?");
    System.out.println(response);

    response = bot.chat("Show me the help for the calculator service");
    System.out.println(response);
} finally {
    mcpClient.close();
}

在上述程式碼中,我們:

  • 使用簡單的自然語言提示與 MCP 伺服器工具互動。
  • LangChain4j 框架自動處理:
    • 將使用者提示轉換為工具呼叫(如果需要)。
    • 根據 LLM 的決定呼叫適當的 MCP 工具。
    • 管理 LLM 和 MCP 伺服器之間的對話流程。
  • bot.chat() 方法返回自然語言響應,可能包括 MCP 工具執行的結果。
  • 此方法提供了一個無縫的使用者體驗,使用者不需要了解底層的 MCP 實現。

完整程式碼示例:

public class LangChain4jClient {
    
    public static void main(String[] args) throws Exception {        ChatLanguageModel model = OpenAiOfficialChatModel.builder()
                .isGitHubModels(true)
                .apiKey(System.getenv("GITHUB_TOKEN"))
                .timeout(Duration.ofSeconds(60))
                .modelName("gpt-4.1-nano")
                .timeout(Duration.ofSeconds(60))
                .build();

        McpTransport transport = new HttpMcpTransport.Builder()
                .sseUrl("http://localhost:8080/sse")
                .timeout(Duration.ofSeconds(60))
                .logRequests(true)
                .logResponses(true)
                .build();

        McpClient mcpClient = new DefaultMcpClient.Builder()
                .transport(transport)
                .build();

        ToolProvider toolProvider = McpToolProvider.builder()
                .mcpClients(List.of(mcpClient))
                .build();

        Bot bot = AiServices.builder(Bot.class)
                .chatLanguageModel(model)
                .toolProvider(toolProvider)
                .build();

        try {
            String response = bot.chat("Calculate the sum of 24.5 and 17.3 using the calculator service");
            System.out.println(response);

            response = bot.chat("What's the square root of 144?");
            System.out.println(response);

            response = bot.chat("Show me the help for the calculator service");
            System.out.println(response);
        } finally {
            mcpClient.close();
        }
    }
}

Rust

這是主要工作所在。我們將使用初始使用者提示呼叫 LLM,然後處理響應以查看是否需要呼叫任何工具。如果需要,我們將呼叫這些工具並繼續與 LLM 的對話,直到不再需要工具呼叫並獲得最終響應。 我們將定義一個函式來處理 LLM 的呼叫。將以下函式新增到你的 main.rs 檔案中:

async fn call_llm(
    client: &Client<OpenAIConfig>,
    messages: &[Value],
    tools: &ListToolsResult,
) -> Result<Value, Box<dyn Error>> {
    let response = client
        .completions()
        .create_byot(json!({
            "messages": messages,
            "model": "openai/gpt-4.1",
            "tools": format_tools(tools).await?,
        }))
        .await?;
    Ok(response)
}

這個函式會接收 LLM 客戶端、一組訊息(包括使用者的提示)、來自 MCP 伺服器的工具,並向 LLM 發送請求,最後回傳回應。

LLM 的回應會包含一個 choices 陣列。我們需要處理這個結果,檢查是否有任何 tool_calls 存在。這可以讓我們知道 LLM 是否請求呼叫特定工具並傳遞參數。將以下程式碼新增到 main.rs 檔案的底部,定義一個函式來處理 LLM 的回應:

async fn process_llm_response(
    llm_response: &Value,
    mcp_client: &RunningService<RoleClient, ()>,
    openai_client: &Client<OpenAIConfig>,
    mcp_tools: &ListToolsResult,
    messages: &mut Vec<Value>,
) -> Result<(), Box<dyn Error>> {
    let Some(message) = llm_response
        .get("choices")
        .and_then(|c| c.as_array())
        .and_then(|choices| choices.first())
        .and_then(|choice| choice.get("message"))
    else {
        return Ok(());
    };

    // Print content if available
    if let Some(content) = message.get("content").and_then(|c| c.as_str()) {
        println!("🤖 {}", content);
    }

    // Handle tool calls
    if let Some(tool_calls) = message.get("tool_calls").and_then(|tc| tc.as_array()) {
        messages.push(message.clone()); // Add assistant message

        // Execute each tool call
        for tool_call in tool_calls {
            let (tool_id, name, args) = extract_tool_call_info(tool_call)?;
            println!("⚡ Calling tool: {}", name);

            let result = mcp_client
                .call_tool(CallToolRequestParam {
                    name: name.into(),
                    arguments: serde_json::from_str::<Value>(&args)?.as_object().cloned(),
                })
                .await?;

            // Add tool result to messages
            messages.push(json!({
                "role": "tool",
                "tool_call_id": tool_id,
                "content": serde_json::to_string_pretty(&result)?
            }));
        }

        // Continue conversation with tool results
        let response = call_llm(openai_client, messages, mcp_tools).await?;
        Box::pin(process_llm_response(
            &response,
            mcp_client,
            openai_client,
            mcp_tools,
            messages,
        ))
        .await?;
    }
    Ok(())
}

如果存在 tool_calls,它會提取工具資訊,向 MCP 伺服器發送工具請求,並將結果新增到對話訊息中。接著,它會繼續與 LLM 進行對話,並將助理的回應和工具呼叫結果更新到訊息中。

為了提取 LLM 返回的 MCP 呼叫所需的工具呼叫資訊,我們將新增另一個輔助函式來提取進行呼叫所需的一切。將以下程式碼新增到 main.rs 檔案的底部:

fn extract_tool_call_info(tool_call: &Value) -> Result<(String, String, String), Box<dyn Error>> {
    let tool_id = tool_call
        .get("id")
        .and_then(|id| id.as_str())
        .unwrap_or("")
        .to_string();
    let function = tool_call.get("function").ok_or("Missing function")?;
    let name = function
        .get("name")
        .and_then(|n| n.as_str())
        .unwrap_or("")
        .to_string();
    let args = function
        .get("arguments")
        .and_then(|a| a.as_str())
        .unwrap_or("{}")
        .to_string();
    Ok((tool_id, name, args))
}

當所有部分都準備就緒後,我們現在可以處理初始使用者提示並呼叫 LLM。更新你的 main 函式,加入以下程式碼:

// LLM conversation with tool calls
let response = call_llm(&openai_client, &messages, &tools).await?;
process_llm_response(
    &response,
    &mcp_client,
    &openai_client,
    &tools,
    &mut messages,
)
.await?;

這段程式碼會使用初始使用者提示來查詢 LLM,要求計算兩個數字的總和,並處理回應以動態處理工具呼叫。

太棒了,你完成了!

作業

使用練習中的程式碼,擴展伺服器並新增更多工具。接著,建立一個包含 LLM 的客戶端(如練習中所示),並使用不同的提示進行測試,確保所有伺服器工具都能被動態呼叫。這種建立客戶端的方式能提供終端使用者更好的使用體驗,因為他們可以使用提示,而不需要輸入精確的客戶端指令,甚至不會察覺 MCP 伺服器的存在。

解答

解答

關鍵要點

  • 在客戶端中加入 LLM,能為使用者提供更好的與 MCP 伺服器互動的方式。
  • 你需要將 MCP 伺服器的回應轉換成 LLM 能理解的格式。

範例

其他資源

下一步

免責聲明
本文檔使用 AI 翻譯服務 Co-op Translator 進行翻譯。儘管我們努力確保準確性,但請注意,自動翻譯可能包含錯誤或不準確之處。原始語言的文件應被視為權威來源。對於關鍵信息,建議使用專業人工翻譯。我們對因使用此翻譯而引起的任何誤解或錯誤解釋不承擔責任。