Fix MCP schema mismatches and argument errors, highlight rejected connections and show average hold time
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
570ac20a5c
commit
e86518cab1
10 files changed
+272
-17
No files matched your search
@@ -0,0 +1,45 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Flypaper.Web.Mcp;
|
||||
|
||||
public static class McpArgumentCheck
|
||||
{
|
||||
public static string? FindProblem(JsonElement inputSchema, IReadOnlyDictionary<string, JsonElement>? arguments)
|
||||
{
|
||||
var properties = inputSchema.TryGetProperty("properties", out var schemaProperties)
|
||||
? schemaProperties.EnumerateObject().ToDictionary(p => p.Name, p => p.Value, StringComparer.Ordinal)
|
||||
: [];
|
||||
var required = inputSchema.TryGetProperty("required", out var schemaRequired)
|
||||
? schemaRequired.EnumerateArray().Select(r => r.GetString()!).ToList()
|
||||
: [];
|
||||
var given = arguments?.Keys.ToList() ?? [];
|
||||
|
||||
var unknown = given.Where(name => !properties.ContainsKey(name)).ToList();
|
||||
var missing = required.Where(name => !given.Contains(name, StringComparer.Ordinal)).ToList();
|
||||
if (unknown.Count == 0 && missing.Count == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var problems = new List<string>();
|
||||
if (unknown.Count == 1 && missing.Count == 1)
|
||||
{
|
||||
problems.Add($"unknown parameter '{unknown[0]}'; expected '{missing[0]}'{Values(properties[missing[0]])}");
|
||||
}
|
||||
else
|
||||
{
|
||||
problems.AddRange(unknown.Select(name => $"unknown parameter '{name}'"));
|
||||
problems.AddRange(missing.Select(name => $"missing required parameter '{name}'{Values(properties[name])}"));
|
||||
}
|
||||
|
||||
var allowed = properties.Count == 0
|
||||
? "none"
|
||||
: string.Join(", ", properties.Select(p => required.Contains(p.Key) ? $"{p.Key} (required)" : p.Key));
|
||||
return $"{string.Join("; ", problems)}. Parameters: {allowed}.";
|
||||
}
|
||||
|
||||
private static string Values(JsonElement property) =>
|
||||
property.TryGetProperty("enum", out var values)
|
||||
? $" ({string.Join("|", values.EnumerateArray().Select(v => v.GetString()))})"
|
||||
: "";
|
||||
}
|
||||
Reference in new issue
Block a user