Remove McpClient.Enumerate*Async methods (#1060)
This commit is contained in:
parent
ce5bb3cf7f
commit
627de76ca6
6 changed files with 4 additions and 173 deletions
|
|
@ -131,39 +131,6 @@ public abstract partial class McpClient : McpSession
|
||||||
return tools;
|
return tools;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates an enumerable for asynchronously enumerating all available tools from the server.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="serializerOptions">The serializer options governing tool parameter serialization. If null, the default options are used.</param>
|
|
||||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
|
||||||
/// <returns>An asynchronous sequence of all available tools as <see cref="McpClientTool"/> instances.</returns>
|
|
||||||
public async IAsyncEnumerable<McpClientTool> EnumerateToolsAsync(
|
|
||||||
JsonSerializerOptions? serializerOptions = null,
|
|
||||||
[EnumeratorCancellation] CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
serializerOptions ??= McpJsonUtilities.DefaultOptions;
|
|
||||||
serializerOptions.MakeReadOnly();
|
|
||||||
|
|
||||||
string? cursor = null;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
var toolResults = await SendRequestAsync(
|
|
||||||
RequestMethods.ToolsList,
|
|
||||||
new() { Cursor = cursor },
|
|
||||||
McpJsonUtilities.JsonContext.Default.ListToolsRequestParams,
|
|
||||||
McpJsonUtilities.JsonContext.Default.ListToolsResult,
|
|
||||||
cancellationToken: cancellationToken).ConfigureAwait(false);
|
|
||||||
|
|
||||||
foreach (var tool in toolResults.Tools)
|
|
||||||
{
|
|
||||||
yield return new McpClientTool(this, tool, serializerOptions);
|
|
||||||
}
|
|
||||||
|
|
||||||
cursor = toolResults.NextCursor;
|
|
||||||
}
|
|
||||||
while (cursor is not null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Retrieves a list of available prompts from the server.
|
/// Retrieves a list of available prompts from the server.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -196,34 +163,6 @@ public abstract partial class McpClient : McpSession
|
||||||
return prompts;
|
return prompts;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates an enumerable for asynchronously enumerating all available prompts from the server.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
|
||||||
/// <returns>An asynchronous sequence of all available prompts as <see cref="McpClientPrompt"/> instances.</returns>
|
|
||||||
public async IAsyncEnumerable<McpClientPrompt> EnumeratePromptsAsync(
|
|
||||||
[EnumeratorCancellation] CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
string? cursor = null;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
var promptResults = await SendRequestAsync(
|
|
||||||
RequestMethods.PromptsList,
|
|
||||||
new() { Cursor = cursor },
|
|
||||||
McpJsonUtilities.JsonContext.Default.ListPromptsRequestParams,
|
|
||||||
McpJsonUtilities.JsonContext.Default.ListPromptsResult,
|
|
||||||
cancellationToken: cancellationToken).ConfigureAwait(false);
|
|
||||||
|
|
||||||
foreach (var prompt in promptResults.Prompts)
|
|
||||||
{
|
|
||||||
yield return new(this, prompt);
|
|
||||||
}
|
|
||||||
|
|
||||||
cursor = promptResults.NextCursor;
|
|
||||||
}
|
|
||||||
while (cursor is not null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Retrieves a specific prompt from the MCP server.
|
/// Retrieves a specific prompt from the MCP server.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -284,34 +223,6 @@ public abstract partial class McpClient : McpSession
|
||||||
return resourceTemplates;
|
return resourceTemplates;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates an enumerable for asynchronously enumerating all available resource templates from the server.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
|
||||||
/// <returns>An asynchronous sequence of all available resource templates as <see cref="ResourceTemplate"/> instances.</returns>
|
|
||||||
public async IAsyncEnumerable<McpClientResourceTemplate> EnumerateResourceTemplatesAsync(
|
|
||||||
[EnumeratorCancellation] CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
string? cursor = null;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
var templateResults = await SendRequestAsync(
|
|
||||||
RequestMethods.ResourcesTemplatesList,
|
|
||||||
new() { Cursor = cursor },
|
|
||||||
McpJsonUtilities.JsonContext.Default.ListResourceTemplatesRequestParams,
|
|
||||||
McpJsonUtilities.JsonContext.Default.ListResourceTemplatesResult,
|
|
||||||
cancellationToken: cancellationToken).ConfigureAwait(false);
|
|
||||||
|
|
||||||
foreach (var templateResult in templateResults.ResourceTemplates)
|
|
||||||
{
|
|
||||||
yield return new McpClientResourceTemplate(this, templateResult);
|
|
||||||
}
|
|
||||||
|
|
||||||
cursor = templateResults.NextCursor;
|
|
||||||
}
|
|
||||||
while (cursor is not null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Retrieves a list of available resources from the server.
|
/// Retrieves a list of available resources from the server.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -345,34 +256,6 @@ public abstract partial class McpClient : McpSession
|
||||||
return resources;
|
return resources;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates an enumerable for asynchronously enumerating all available resources from the server.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
|
||||||
/// <returns>An asynchronous sequence of all available resources as <see cref="Resource"/> instances.</returns>
|
|
||||||
public async IAsyncEnumerable<McpClientResource> EnumerateResourcesAsync(
|
|
||||||
[EnumeratorCancellation] CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
string? cursor = null;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
var resourceResults = await SendRequestAsync(
|
|
||||||
RequestMethods.ResourcesList,
|
|
||||||
new() { Cursor = cursor },
|
|
||||||
McpJsonUtilities.JsonContext.Default.ListResourcesRequestParams,
|
|
||||||
McpJsonUtilities.JsonContext.Default.ListResourcesResult,
|
|
||||||
cancellationToken: cancellationToken).ConfigureAwait(false);
|
|
||||||
|
|
||||||
foreach (var resource in resourceResults.Resources)
|
|
||||||
{
|
|
||||||
yield return new McpClientResource(this, resource);
|
|
||||||
}
|
|
||||||
|
|
||||||
cursor = resourceResults.NextCursor;
|
|
||||||
}
|
|
||||||
while (cursor is not null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Reads a resource from the server.
|
/// Reads a resource from the server.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,7 @@ namespace ModelContextProtocol.Client;
|
||||||
/// <para>
|
/// <para>
|
||||||
/// This class provides a client-side wrapper around a prompt defined on an MCP server. It allows
|
/// This class provides a client-side wrapper around a prompt defined on an MCP server. It allows
|
||||||
/// retrieving the prompt's content by sending a request to the server with optional arguments.
|
/// retrieving the prompt's content by sending a request to the server with optional arguments.
|
||||||
/// Instances of this class are typically obtained by calling <see cref="McpClient.ListPromptsAsync"/>
|
/// Instances of this class are typically obtained by calling <see cref="McpClient.ListPromptsAsync"/>.
|
||||||
/// or <see cref="McpClient.EnumeratePromptsAsync"/>.
|
|
||||||
/// </para>
|
/// </para>
|
||||||
/// <para>
|
/// <para>
|
||||||
/// Each prompt has a name and optionally a description, and it can be invoked with arguments
|
/// Each prompt has a name and optionally a description, and it can be invoked with arguments
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,7 @@ namespace ModelContextProtocol.Client;
|
||||||
/// <para>
|
/// <para>
|
||||||
/// This class provides a client-side wrapper around a resource defined on an MCP server. It allows
|
/// This class provides a client-side wrapper around a resource defined on an MCP server. It allows
|
||||||
/// retrieving the resource's content by sending a request to the server with the resource's URI.
|
/// retrieving the resource's content by sending a request to the server with the resource's URI.
|
||||||
/// Instances of this class are typically obtained by calling <see cref="McpClient.ListResourcesAsync"/>
|
/// Instances of this class are typically obtained by calling <see cref="McpClient.ListResourcesAsync"/>.
|
||||||
/// or <see cref="McpClient.EnumerateResourcesAsync"/>.
|
|
||||||
/// </para>
|
/// </para>
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public sealed class McpClientResource
|
public sealed class McpClientResource
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,7 @@ namespace ModelContextProtocol.Client;
|
||||||
/// <para>
|
/// <para>
|
||||||
/// This class provides a client-side wrapper around a resource template defined on an MCP server. It allows
|
/// This class provides a client-side wrapper around a resource template defined on an MCP server. It allows
|
||||||
/// retrieving the resource template's content by sending a request to the server with the resource's URI.
|
/// retrieving the resource template's content by sending a request to the server with the resource's URI.
|
||||||
/// Instances of this class are typically obtained by calling <see cref="McpClient.ListResourceTemplatesAsync"/>
|
/// Instances of this class are typically obtained by calling <see cref="McpClient.ListResourceTemplatesAsync"/>.
|
||||||
/// or <see cref="McpClient.EnumerateResourceTemplatesAsync"/>.
|
|
||||||
/// </para>
|
/// </para>
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public sealed class McpClientResourceTemplate
|
public sealed class McpClientResourceTemplate
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ namespace ModelContextProtocol.Client;
|
||||||
/// </para>
|
/// </para>
|
||||||
/// <para>
|
/// <para>
|
||||||
/// Typically, you would get instances of this class by calling the <see cref="McpClient.ListToolsAsync"/>
|
/// Typically, you would get instances of this class by calling the <see cref="McpClient.ListToolsAsync"/>
|
||||||
/// or <see cref="McpClient.EnumerateToolsAsync"/> extension methods on an <see cref="McpClient"/> instance.
|
/// method on an <see cref="McpClient"/> instance.
|
||||||
/// </para>
|
/// </para>
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public sealed class McpClientTool : AIFunction
|
public sealed class McpClientTool : AIFunction
|
||||||
|
|
|
||||||
|
|
@ -286,55 +286,6 @@ public class McpClientTests : ClientServerTestBase
|
||||||
Assert.False(valuesSetViaOptions.ProtocolTool.Annotations?.OpenWorldHint);
|
Assert.False(valuesSetViaOptions.ProtocolTool.Annotations?.OpenWorldHint);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task EnumerateToolsAsync_AllToolsReturned()
|
|
||||||
{
|
|
||||||
await using McpClient client = await CreateMcpClientForServer();
|
|
||||||
|
|
||||||
await foreach (var tool in client.EnumerateToolsAsync(cancellationToken: TestContext.Current.CancellationToken))
|
|
||||||
{
|
|
||||||
if (tool.Name == "Method4")
|
|
||||||
{
|
|
||||||
var result = await tool.InvokeAsync(new() { ["i"] = 42 }, TestContext.Current.CancellationToken);
|
|
||||||
Assert.Contains("Method4 Result 42", result?.ToString());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Assert.Fail("Couldn't find target method");
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task EnumerateToolsAsync_FlowsJsonSerializerOptions()
|
|
||||||
{
|
|
||||||
JsonSerializerOptions options = new(JsonSerializerOptions.Default);
|
|
||||||
await using McpClient client = await CreateMcpClientForServer();
|
|
||||||
bool hasTools = false;
|
|
||||||
|
|
||||||
await foreach (var tool in client.EnumerateToolsAsync(options, TestContext.Current.CancellationToken))
|
|
||||||
{
|
|
||||||
Assert.Same(options, tool.JsonSerializerOptions);
|
|
||||||
hasTools = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var tool in await client.ListToolsAsync(options, TestContext.Current.CancellationToken))
|
|
||||||
{
|
|
||||||
Assert.Same(options, tool.JsonSerializerOptions);
|
|
||||||
}
|
|
||||||
|
|
||||||
Assert.True(hasTools);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task EnumerateToolsAsync_HonorsJsonSerializerOptions()
|
|
||||||
{
|
|
||||||
JsonSerializerOptions emptyOptions = new() { TypeInfoResolver = JsonTypeInfoResolver.Combine() };
|
|
||||||
await using McpClient client = await CreateMcpClientForServer();
|
|
||||||
|
|
||||||
var tool = (await client.ListToolsAsync(emptyOptions, TestContext.Current.CancellationToken)).First();
|
|
||||||
await Assert.ThrowsAsync<NotSupportedException>(async () => await tool.InvokeAsync(new() { ["i"] = 42 }, TestContext.Current.CancellationToken));
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task SendRequestAsync_HonorsJsonSerializerOptions()
|
public async Task SendRequestAsync_HonorsJsonSerializerOptions()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue