Logging (#11)
* Logging Fixed bug in stdio transport argument promotion * Logging Fixed bug in stdio transport argument promotion * Version bump
This commit is contained in:
parent
67bcba5af4
commit
4b8df6638e
1 changed files with 674 additions and 298 deletions
|
|
@ -1,51 +1,26 @@
|
|||
using McpDotNet.Client;
|
||||
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;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace McpDotNet.Tests;
|
||||
|
||||
public class IntegrationTests
|
||||
public class IntegrationTests : IClassFixture<IntegrationTestFixture>
|
||||
{
|
||||
private readonly McpClientOptions _defaultOptions = new()
|
||||
private readonly IntegrationTestFixture _fixture;
|
||||
|
||||
public IntegrationTests(IntegrationTestFixture fixture)
|
||||
{
|
||||
ClientInfo = new() { Name = "IntegrationTestClient", Version = "1.0.0" }
|
||||
};
|
||||
_fixture = fixture;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ConnectAndPing_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",
|
||||
}
|
||||
};
|
||||
|
||||
// Inject the mock transport into the factory
|
||||
var factory = new McpClientFactory(
|
||||
new[] { config },
|
||||
_defaultOptions
|
||||
);
|
||||
|
||||
|
||||
// Act
|
||||
var client = await factory.GetClientAsync("everything");
|
||||
var client = await _fixture.Factory.GetClientAsync("everything");
|
||||
await client.PingAsync(CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
|
|
@ -56,25 +31,9 @@ public class IntegrationTests
|
|||
public async Task ListTools_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
|
||||
);
|
||||
|
||||
|
||||
// act
|
||||
var client = await factory.GetClientAsync("everything");
|
||||
var client = await _fixture.Factory.GetClientAsync("everything");
|
||||
var tools = await client.ListToolsAsync();
|
||||
|
||||
// assert
|
||||
|
|
@ -87,25 +46,9 @@ public class IntegrationTests
|
|||
public async Task CallTool_Stdio_EchoServer()
|
||||
{
|
||||
// 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
|
||||
);
|
||||
|
||||
|
||||
// act
|
||||
var client = await factory.GetClientAsync("everything");
|
||||
var client = await _fixture.Factory.GetClientAsync("everything");
|
||||
var result = await client.CallToolAsync(
|
||||
"echo",
|
||||
new Dictionary<string, object>
|
||||
|
|
@ -126,24 +69,9 @@ public class IntegrationTests
|
|||
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");
|
||||
// act
|
||||
var client = await _fixture.Factory.GetClientAsync("everything");
|
||||
var prompts = await client.ListPromptsAsync();
|
||||
|
||||
// assert
|
||||
|
|
@ -158,24 +86,9 @@ public class IntegrationTests
|
|||
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");
|
||||
// act
|
||||
var client = await _fixture.Factory.GetClientAsync("everything");
|
||||
var result = await client.GetPromptAsync("simple_prompt", null, CancellationToken.None);
|
||||
|
||||
// assert
|
||||
|
|
@ -187,24 +100,9 @@ public class IntegrationTests
|
|||
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");
|
||||
// act
|
||||
var client = await _fixture.Factory.GetClientAsync("everything");
|
||||
var arguments = new Dictionary<string, object>
|
||||
{
|
||||
{ "temperature", "0.7" },
|
||||
|
|
@ -221,24 +119,9 @@ public class IntegrationTests
|
|||
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");
|
||||
// act
|
||||
var client = await _fixture.Factory.GetClientAsync("everything");
|
||||
await Assert.ThrowsAsync<McpClientException>(() =>
|
||||
client.GetPromptAsync("non_existent_prompt", null, CancellationToken.None));
|
||||
}
|
||||
|
|
@ -247,24 +130,9 @@ public class IntegrationTests
|
|||
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");
|
||||
// act
|
||||
var client = await _fixture.Factory.GetClientAsync("everything");
|
||||
|
||||
List<Resource> allResources = new();
|
||||
string? cursor = null;
|
||||
|
|
@ -284,24 +152,9 @@ public class IntegrationTests
|
|||
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");
|
||||
// act
|
||||
var client = await _fixture.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);
|
||||
|
|
@ -315,24 +168,9 @@ public class IntegrationTests
|
|||
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");
|
||||
|
||||
// act
|
||||
var client = await _fixture.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);
|
||||
|
|
@ -354,7 +192,7 @@ public class IntegrationTests
|
|||
TransportOptions = new Dictionary<string, string>
|
||||
{
|
||||
["command"] = "npx",
|
||||
["arguments"] = "-y @modelcontextprotocol/server-everything"
|
||||
["arguments"] = "-y @modelcontextprotocol/server-everything",
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -368,13 +206,15 @@ public class IntegrationTests
|
|||
};
|
||||
|
||||
var factory = new McpClientFactory(
|
||||
new[] { config },
|
||||
options
|
||||
[config],
|
||||
options,
|
||||
_fixture.LoggerFactory
|
||||
);
|
||||
var client = await factory.GetClientAsync("everything");
|
||||
|
||||
// Set up the sampling handler
|
||||
int samplingHandlerCalls = 0;
|
||||
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
|
||||
client.SamplingHandler = async (_) =>
|
||||
{
|
||||
samplingHandlerCalls++;
|
||||
|
|
@ -389,6 +229,7 @@ public class IntegrationTests
|
|||
}
|
||||
};
|
||||
};
|
||||
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
|
||||
|
||||
// Call the server's sampleLLM tool which should trigger our sampling handler
|
||||
var result = await client.CallToolAsync(
|
||||
|
|
@ -404,6 +245,6 @@ public class IntegrationTests
|
|||
Assert.NotNull(result);
|
||||
var textContent = Assert.Single(result.Content);
|
||||
Assert.Equal("text", textContent.Type);
|
||||
Assert.Contains("Test response", textContent.Text);
|
||||
Assert.False(string.IsNullOrEmpty(textContent.Text));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue