Resource capability (#5)

Prompt capability
Pagination support
This commit is contained in:
Peder Holdgaard Pedersen 2025-01-04 19:39:22 +01:00 • committed by GitHub
commit c9ace236a0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,11 +2,13 @@
using McpDotNet.Configuration;
using McpDotNet.Protocol.Messages;
using McpDotNet.Protocol.Transport;
using McpDotNet.Protocol.Types;
using Moq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Channels;
using System.Threading.Tasks;
@ -73,7 +75,7 @@ public class IntegrationTests
// act
var client = await factory.GetClientAsync("everything");
var tools = await client.ListToolsAsync(CancellationToken.None);
var tools = await client.ListToolsAsync();
// assert
Assert.NotNull(tools);
@ -119,4 +121,224 @@ public class IntegrationTests
var textContent = Assert.Single(result.Content.Where(c => c.Type == "text"));
Assert.Equal("Echo: Hello MCP!", textContent.Text);
}
[Fact]
public async Task ListPrompts_Stdio_EverythingServer()
{
// arrange
var config = new McpServerConfig
{
Id = "everything",
Name = "Everything",
TransportType = "stdio",
TransportOptions = new Dictionary<string, string>
{
["command"] = "npx",
["arguments"] = "-y @modelcontextprotocol/server-everything"
}
};
var factory = new McpClientFactory(
new[] { config },
_defaultOptions
);
var client = await factory.GetClientAsync("everything");
var prompts = await client.ListPromptsAsync();
// assert
Assert.NotNull(prompts);
Assert.NotEmpty(prompts.Prompts);
// We could add specific assertions for the known prompts
Assert.Contains(prompts.Prompts, p => p.Name == "simple_prompt");
Assert.Contains(prompts.Prompts, p => p.Name == "complex_prompt");
}
[Fact]
public async Task GetPrompt_Stdio_SimplePrompt()
{
// arrange
var config = new McpServerConfig
{
Id = "everything",
Name = "Everything",
TransportType = "stdio",
TransportOptions = new Dictionary<string, string>
{
["command"] = "npx",
["arguments"] = "-y @modelcontextprotocol/server-everything"
}
};
var factory = new McpClientFactory(
new[] { config },
_defaultOptions
);
var client = await factory.GetClientAsync("everything");
var result = await client.GetPromptAsync("simple_prompt", null, CancellationToken.None);
// assert
Assert.NotNull(result);
Assert.NotEmpty(result.Messages);
}
[Fact]
public async Task GetPrompt_Stdio_ComplexPrompt()
{
// arrange
var config = new McpServerConfig
{
Id = "everything",
Name = "Everything",
TransportType = "stdio",
TransportOptions = new Dictionary<string, string>
{
["command"] = "npx",
["arguments"] = "-y @modelcontextprotocol/server-everything"
}
};
var factory = new McpClientFactory(
new[] { config },
_defaultOptions
);
var client = await factory.GetClientAsync("everything");
var arguments = new Dictionary<string, object>
{
{ "temperature", "0.7" },
{ "style", "formal" }
};
var result = await client.GetPromptAsync("complex_prompt", arguments, CancellationToken.None);
// assert
Assert.NotNull(result);
Assert.NotEmpty(result.Messages);
}
[Fact]
public async Task GetPrompt_NonExistent_ThrowsException()
{
// arrange
var config = new McpServerConfig
{
Id = "everything",
Name = "Everything",
TransportType = "stdio",
TransportOptions = new Dictionary<string, string>
{
["command"] = "npx",
["arguments"] = "-y @modelcontextprotocol/server-everything"
}
};
var factory = new McpClientFactory(
new[] { config },
_defaultOptions
);
var client = await factory.GetClientAsync("everything");
await Assert.ThrowsAsync<McpClientException>(() =>
client.GetPromptAsync("non_existent_prompt", null, CancellationToken.None));
}
[Fact]
public async Task ListResources_Stdio_EverythingServer()
{
// arrange
var config = new McpServerConfig
{
Id = "everything",
Name = "Everything",
TransportType = "stdio",
TransportOptions = new Dictionary<string, string>
{
["command"] = "npx",
["arguments"] = "-y @modelcontextprotocol/server-everything"
}
};
var factory = new McpClientFactory(
new[] { config },
_defaultOptions
);
var client = await factory.GetClientAsync("everything");
List<Resource> allResources = new();
string? cursor = null;
do
{
var resources = await client.ListResourcesAsync(cursor, CancellationToken.None);
allResources.AddRange(resources.Resources);
cursor = resources.NextCursor;
}
while (cursor != null);
// The everything server provides 100 test resources
Assert.Equal(100, allResources.Count);
}
[Fact]
public async Task ReadResource_Stdio_TextResource()
{
// arrange
var config = new McpServerConfig
{
Id = "everything",
Name = "Everything",
TransportType = "stdio",
TransportOptions = new Dictionary<string, string>
{
["command"] = "npx",
["arguments"] = "-y @modelcontextprotocol/server-everything"
}
};
var factory = new McpClientFactory(
new[] { config },
_defaultOptions
);
var client = await factory.GetClientAsync("everything");
// Odd numbered resources are text in the everything server (despite the docs saying otherwise)
// 1 is index 0, which is "even" in the 0-based index
var result = await client.ReadResourceAsync("test://static/resource/1", CancellationToken.None);
Assert.NotNull(result);
Assert.Single(result.Contents);
Assert.NotNull(result.Contents[0].Text);
}
[Fact]
public async Task ReadResource_Stdio_BinaryResource()
{
// arrange
var config = new McpServerConfig
{
Id = "everything",
Name = "Everything",
TransportType = "stdio",
TransportOptions = new Dictionary<string, string>
{
["command"] = "npx",
["arguments"] = "-y @modelcontextprotocol/server-everything"
}
};
var factory = new McpClientFactory(
new[] { config },
_defaultOptions
);
var client = await factory.GetClientAsync("everything");
// Even numbered resources are binary in the everything server (despite the docs saying otherwise)
// 2 is index 1, which is "odd" in the 0-based index
var result = await client.ReadResourceAsync("test://static/resource/2", CancellationToken.None);
Assert.NotNull(result);
Assert.Single(result.Contents);
Assert.NotNull(result.Contents[0].Blob);
}
}