Skip to content

Commit fdd99f7

Browse files
Prevent pagination table from showing up for non list calls
1 parent 344ccf2 commit fdd99f7

File tree

3 files changed

+30
-32
lines changed

3 files changed

+30
-32
lines changed

core/Microsoft.Mcp.Core/src/Areas/Server/Commands/ServiceCollectionExtensions.cs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
using Microsoft.Mcp.Core.Extensions;
1717
using Microsoft.Mcp.Core.Helpers;
1818
using ModelContextProtocol.Protocol;
19-
using ModelContextProtocol.Server;
2019

2120
namespace Microsoft.Mcp.Core.Areas.Server.Commands;
2221

@@ -214,7 +213,7 @@ public static IServiceCollection AddAzureMcpServer(this IServiceCollection servi
214213

215214
var mcpServerOptions = services
216215
.AddOptions<McpServerOptions>()
217-
.Configure<IMcpRuntime, IServerInstructionsProvider, IOptions<McpServerConfiguration>, IEnumerable<McpServerResource>>((mcpServerOptions, mcpRuntime, serverInstructionsProvider, serverConfiguration, resources) =>
216+
.Configure<IMcpRuntime, IServerInstructionsProvider, IOptions<McpServerConfiguration>>((mcpServerOptions, mcpRuntime, serverInstructionsProvider, serverConfiguration) =>
218217
{
219218
var configuration = serverConfiguration.Value;
220219

@@ -224,33 +223,10 @@ public static IServiceCollection AddAzureMcpServer(this IServiceCollection servi
224223
Version = configuration.Version,
225224
};
226225

227-
var resourceList = resources.ToList();
228-
229226
mcpServerOptions.Handlers = new()
230227
{
231228
CallToolHandler = mcpRuntime.CallToolHandler,
232-
ListToolsHandler = mcpRuntime.ListToolsHandler,
233-
ListResourceTemplatesHandler = (request, cancellationToken) =>
234-
{
235-
var templates = resourceList.Select(r => r.ProtocolResourceTemplate).ToList();
236-
return ValueTask.FromResult(new ListResourceTemplatesResult { ResourceTemplates = templates });
237-
},
238-
ReadResourceHandler = (request, cancellationToken) =>
239-
{
240-
var uri = request.Params?.Uri;
241-
if (string.IsNullOrEmpty(uri))
242-
{
243-
return ValueTask.FromResult(new ReadResourceResult { Contents = [] });
244-
}
245-
246-
var resource = resourceList.FirstOrDefault(r => r.IsMatch(uri));
247-
if (resource is null)
248-
{
249-
return ValueTask.FromResult(new ReadResourceResult { Contents = [] });
250-
}
251-
252-
return resource.ReadAsync(request, cancellationToken);
253-
},
229+
ListToolsHandler = mcpRuntime.ListToolsHandler
254230
};
255231

256232
// Add instructions for the server

core/Microsoft.Mcp.Core/src/Areas/Server/Commands/ToolLoading/CommandFactoryToolLoader.cs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Microsoft.Extensions.Options;
99
using Microsoft.Mcp.Core.Areas.Server.Models;
1010
using Microsoft.Mcp.Core.Commands;
11-
using Microsoft.Mcp.Core.Services.Caching.Pagination;
1211
using Microsoft.Mcp.Core.Helpers;
1312
using Microsoft.Mcp.Core.Models;
1413
using Microsoft.Mcp.Core.Models.Command;
@@ -223,7 +222,7 @@ public override async ValueTask<CallToolResult> CallToolHandler(RequestContext<C
223222
var jsonResponse = JsonSerializer.Serialize(commandResponse, ModelsJsonContext.Default.CommandResponse);
224223
var isError = commandResponse.Status < HttpStatusCode.OK || commandResponse.Status >= HttpStatusCode.Ambiguous;
225224

226-
return new CallToolResult
225+
var callResult = new CallToolResult
227226
{
228227
Content = [
229228
new TextContentBlock {
@@ -232,6 +231,12 @@ public override async ValueTask<CallToolResult> CallToolHandler(RequestContext<C
232231
],
233232
IsError = isError
234233
};
234+
235+
// Propagate _meta.ui from the command response to the CallToolResult.Meta
236+
// so that the host (e.g. VS Code) can render the MCP App inline.
237+
callResult.Meta = ExtractUiMeta(jsonResponse);
238+
239+
return callResult;
235240
}
236241
catch (Exception ex)
237242
{
@@ -288,7 +293,6 @@ private static Tool GetTool(string fullName, IBaseCommand command)
288293
{
289294
meta ??= new();
290295
meta["PaginationHint"] = metadata.SupportsPagination;
291-
meta["ui"] = new JsonObject { ["resourceUri"] = TableAppResource.UriPrefix };
292296
}
293297
tool.Meta = meta;
294298

@@ -331,4 +335,24 @@ protected override ValueTask DisposeAsyncCore()
331335
// CommandFactoryToolLoader doesn't create or manage disposable resources
332336
return ValueTask.CompletedTask;
333337
}
338+
339+
/// <summary>
340+
/// Extracts _meta.ui from the serialized command response JSON and returns it
341+
/// as a <see cref="JsonObject"/> suitable for <see cref="CallToolResult.Meta"/>.
342+
/// Returns <c>null</c> when no ui metadata is present.
343+
/// </summary>
344+
private static JsonObject? ExtractUiMeta(string jsonResponse)
345+
{
346+
var doc = JsonNode.Parse(jsonResponse);
347+
var uiNode = doc?["results"]?["_meta"]?["ui"];
348+
if (uiNode is null)
349+
{
350+
return null;
351+
}
352+
353+
return new JsonObject
354+
{
355+
["ui"] = uiNode.DeepClone(),
356+
};
357+
}
334358
}

core/Microsoft.Mcp.Core/src/Areas/Server/Commands/ToolLoading/NamespaceToolLoader.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using Microsoft.Mcp.Core.Helpers;
1414
using Microsoft.Mcp.Core.Models;
1515
using Microsoft.Mcp.Core.Models.Command;
16-
using Microsoft.Mcp.Core.Services.Caching.Pagination;
1716
using ModelContextProtocol;
1817
using ModelContextProtocol.Protocol;
1918

@@ -150,13 +149,12 @@ Sub commands are routed to MCP servers that require specific fields inside the "
150149
},
151150
};
152151

153-
// Add UI resource URI if any child command supports pagination
152+
// Add pagination hint if any child command supports pagination
154153
if (!AllToolsInGroupMatch(meta => !meta.SupportsPagination, group))
155154
{
156155
tool.Meta = new JsonObject
157156
{
158157
["PaginationHint"] = true,
159-
["ui"] = new JsonObject { ["resourceUri"] = TableAppResource.UriPrefix },
160158
};
161159
}
162160

0 commit comments

Comments
 (0)