You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Semantic Kernel调用远程MCP Server时参数格式错误问题求助

Semantic Kernel调用远程MCP Server时参数格式错误问题求助

我现在在尝试用Semantic Kernel连接远程MCP Server,遇到了参数格式的问题——Semantic Kernel发送的JSON请求里,searchRequest本该是一个嵌套对象,但它却被序列化成了字符串,导致MCP Server无法正确解析请求。

正确的请求格式

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "GetDestinationList",
    "arguments": {
      "searchRequest": {
        "searchValue": "Istanbul"
      }
    }
  }
}

Semantic Kernel实际发送的错误请求

{
  "jsonrpc": "2.0",
  "id": 3,
  "method": "tools/call",
  "params": {
    "name": "GetDestinationList",
    "arguments": {
      "searchRequest": "{\"searchValue\":\"istanbul\"}"
    }
  }
}

我排查后发现,问题大概率出在tools.Select(aiFunction => aiFunction.AsKernelFunction())这段代码上。在将AI工具转换为Kernel Function的过程中,参数的嵌套结构被错误处理了——我调试时看到工具的JSON Schema是完全正确的,但转换后发送的请求就把嵌套对象变成了字符串。

调试时看到的正确工具Schema

{
  "type": "object",
  "condition": "Get destinations by name as DestinationId and DestinationName",
  "properties": {
    "searchRequest": {
      "type": "object",
      "properties": {
        "searchValue": {
          "type": "string"
        }
      }
    }
  },
  "required": [ "searchRequest" ]
}

我的完整实现代码

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using ModelContextProtocol.Client;
using System.Text.Json;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// Configure Semantic Kernel with OpenAI
builder.Services.AddKernel().AddOpenAIChatCompletion(
    modelId: "gpt-4.1",
    apiKey: "xxxxxxxx"
);

// Register MainAgentManager
builder.Services.AddScoped<MainAgentManager>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();

var kernel = app.Services.GetRequiredService<Kernel>();

SseClientTransport sseTransport = new(new() { Endpoint = new Uri("mcp.xxxxxx.com"), Name = "sseTransport"});
IMcpClient mcpClient = await McpClientFactory.CreateAsync(sseTransport);

// Suppress the diagnostic warning SKEXP0001 by explicitly disabling it for the specific line of code.
// This is a temporary measure to proceed with the current implementation while acknowledging that the API is subject to change.
#pragma warning disable SKEXP0001
kernel.Plugins.AddFromFunctions("keytool", tools.Select(aiFunction => aiFunction.AsKernelFunction()));
#pragma warning restore SKEXP0001

var executionSettings = new OpenAIPromptExecutionSettings
{
    Temperature = 0,
    FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
};

var prompt = "What is the destination id of istanbul";
var result = await kernel.InvokePromptAsync(prompt, new(executionSettings)).ConfigureAwait(false);

app.Run();

有没有遇到过类似问题的朋友?或者熟悉Semantic Kernel工具转换逻辑的大佬,能帮忙看看怎么修复这个问题,让Semantic Kernel发送正确格式的请求吗?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.07 11:37:59