Add middleware and authz support for server-side handlers (#733)
This commit is contained in:
parent
0f0aad020b
commit
d344c65120
1 changed files with 2871 additions and 435 deletions
|
|
@ -1,4 +1,4 @@
|
|||
using Microsoft.Extensions.AI;
|
||||
using Microsoft.Extensions.AI;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using ModelContextProtocol.Protocol;
|
||||
|
|
@ -15,6 +15,16 @@ namespace ModelContextProtocol.Tests.Server;
|
|||
|
||||
public class McpServerPromptTests
|
||||
{
|
||||
private static JsonRpcRequest CreateTestJsonRpcRequest()
|
||||
{
|
||||
return new JsonRpcRequest
|
||||
{
|
||||
Id = new RequestId("test-id"),
|
||||
Method = "test/method",
|
||||
Params = null
|
||||
};
|
||||
}
|
||||
|
||||
public McpServerPromptTests()
|
||||
{
|
||||
#if !NET
|
||||
|
|
@ -46,7 +56,7 @@ public class McpServerPromptTests
|
|||
Assert.DoesNotContain("server", prompt.ProtocolPrompt.Arguments?.Select(a => a.Name) ?? []);
|
||||
|
||||
var result = await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(mockServer.Object),
|
||||
new RequestContext<GetPromptRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.NotNull(result);
|
||||
Assert.NotNull(result.Messages);
|
||||
|
|
@ -75,7 +85,7 @@ public class McpServerPromptTests
|
|||
}, new() { Services = services });
|
||||
|
||||
var result = await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(mockServer.Object),
|
||||
new RequestContext<GetPromptRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.NotNull(result);
|
||||
Assert.NotNull(result.Messages);
|
||||
|
|
@ -125,11 +135,11 @@ public class McpServerPromptTests
|
|||
Assert.DoesNotContain("actualMyService", prompt.ProtocolPrompt.Arguments?.Select(a => a.Name) ?? []);
|
||||
|
||||
await Assert.ThrowsAnyAsync<ArgumentException>(async () => await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
TestContext.Current.CancellationToken));
|
||||
|
||||
var result = await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object) { Services = services },
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object, CreateTestJsonRpcRequest()) { Services = services },
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.Equal("Hello", Assert.IsType<TextContentBlock>(result.Messages[0].Content).Text);
|
||||
}
|
||||
|
|
@ -150,7 +160,7 @@ public class McpServerPromptTests
|
|||
}, new() { Services = services });
|
||||
|
||||
var result = await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.Equal("Hello", Assert.IsType<TextContentBlock>(result.Messages[0].Content).Text);
|
||||
}
|
||||
|
|
@ -163,7 +173,7 @@ public class McpServerPromptTests
|
|||
_ => new DisposablePromptType());
|
||||
|
||||
var result = await prompt1.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.Equal("disposals:1", Assert.IsType<TextContentBlock>(result.Messages[0].Content).Text);
|
||||
}
|
||||
|
|
@ -176,7 +186,7 @@ public class McpServerPromptTests
|
|||
_ => new AsyncDisposablePromptType());
|
||||
|
||||
var result = await prompt1.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.Equal("asyncDisposals:1", Assert.IsType<TextContentBlock>(result.Messages[0].Content).Text);
|
||||
}
|
||||
|
|
@ -189,7 +199,7 @@ public class McpServerPromptTests
|
|||
_ => new AsyncDisposableAndDisposablePromptType());
|
||||
|
||||
var result = await prompt1.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.Equal("disposals:0, asyncDisposals:1", Assert.IsType<TextContentBlock>(result.Messages[0].Content).Text);
|
||||
}
|
||||
|
|
@ -205,7 +215,7 @@ public class McpServerPromptTests
|
|||
});
|
||||
|
||||
var actual = await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.Same(expected, actual);
|
||||
|
|
@ -222,7 +232,7 @@ public class McpServerPromptTests
|
|||
});
|
||||
|
||||
var actual = await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.NotNull(actual);
|
||||
|
|
@ -248,7 +258,7 @@ public class McpServerPromptTests
|
|||
});
|
||||
|
||||
var actual = await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.NotNull(actual);
|
||||
|
|
@ -260,7 +270,7 @@ public class McpServerPromptTests
|
|||
[Fact]
|
||||
public async Task CanReturnPromptMessages()
|
||||
{
|
||||
IList<PromptMessage> expected =
|
||||
IList<PromptMessage> expected =
|
||||
[
|
||||
new()
|
||||
{
|
||||
|
|
@ -280,7 +290,7 @@ public class McpServerPromptTests
|
|||
});
|
||||
|
||||
var actual = await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.NotNull(actual);
|
||||
|
|
@ -307,7 +317,7 @@ public class McpServerPromptTests
|
|||
});
|
||||
|
||||
var actual = await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.NotNull(actual);
|
||||
|
|
@ -339,7 +349,7 @@ public class McpServerPromptTests
|
|||
});
|
||||
|
||||
var actual = await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.NotNull(actual);
|
||||
|
|
@ -360,7 +370,7 @@ public class McpServerPromptTests
|
|||
});
|
||||
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(async () => await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
TestContext.Current.CancellationToken));
|
||||
}
|
||||
|
||||
|
|
@ -373,7 +383,7 @@ public class McpServerPromptTests
|
|||
});
|
||||
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(async () => await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
TestContext.Current.CancellationToken));
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue