-
Notifications
You must be signed in to change notification settings - Fork 49
Add MCP endpoint for audit and failed messages #5391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
WilliamBZA
wants to merge
35
commits into
master
Choose a base branch
from
add-mcp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
2189e04
Add failed message MCP server
WilliamBZA 1c80991
Add feature flag check for MCP
WilliamBZA 7b47a30
Update to v1.1.0 of ModelContextProtocol.AspNetCore
WilliamBZA 5c5645e
Turn MCP off by default
WilliamBZA 2c00d5e
Put packages in alphabetical order
WilliamBZA e4ab0ea
Update approvals
WilliamBZA 4c8ecda
Don't pass the full settings object in
WilliamBZA add9235
Add failed message MCP server
WilliamBZA ae22f8a
Use /mcp as the route
WilliamBZA a160b4d
Add MCP for audit
WilliamBZA 5da1181
Remove duplicate project reference
WilliamBZA 49d946b
Update approvals
WilliamBZA 312e2a7
Add test
WilliamBZA fbc8a00
Move to approvals
WilliamBZA 481de0d
Move tests to use POCOs
WilliamBZA 7e014b5
Move packages to be in alphabetical order
WilliamBZA 9bf1cbc
Add unit tests for MCP
WilliamBZA fe4d19a
Add primary acceptance tests
WilliamBZA 0efd5dc
Update MCP descriptions
WilliamBZA 97b03b6
Add approval file
WilliamBZA a12b134
Update audit approval files
WilliamBZA 92a4624
Update raven audit approval
WilliamBZA 23165d4
Order approval files
WilliamBZA 2a920c8
Add logging
WilliamBZA a788827
Improve MCP metadata guidance for AI clients (#5402)
danielmarbach 06d9e45
Align wording order
danielmarbach 5a43098
Return typed structured content for MCP tools while preserving text c…
danielmarbach 537bc13
Add MCP source-generated JSON contexts
danielmarbach 2959499
Removed unused options type
danielmarbach eb44439
Only enable destructive actions if explicitly set
WilliamBZA 7c71b5d
Don't use backing field
WilliamBZA 72996f4
Don't load tools from assembly
WilliamBZA 83fb9b5
Add MCP environment data provider
johnsimons 456b966
Forgot to register it!
johnsimons 732568a
Merge pull request #5413 from Particular/john/data
johnsimons File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
214 changes: 214 additions & 0 deletions
214
src/ServiceControl.Audit.AcceptanceTests/Mcp/When_mcp_server_is_enabled.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| namespace ServiceControl.Audit.AcceptanceTests.Mcp; | ||
|
|
||
| using System.Linq; | ||
| using System.Net; | ||
| using System.Net.Http; | ||
| using System.Net.Http.Json; | ||
| using System.Text.Json; | ||
| using System.Threading.Tasks; | ||
| using AcceptanceTesting; | ||
| using AcceptanceTesting.EndpointTemplates; | ||
| using Audit.Auditing.MessagesView; | ||
| using NServiceBus; | ||
| using NServiceBus.AcceptanceTesting; | ||
| using NServiceBus.AcceptanceTesting.Customization; | ||
| using NServiceBus.Settings; | ||
| using NUnit.Framework; | ||
|
|
||
| class When_mcp_server_is_enabled : AcceptanceTest | ||
| { | ||
| [SetUp] | ||
| public void EnableMcp() => SetSettings = s => s.EnableMcpServer = true; | ||
|
|
||
| [Test] | ||
| public async Task Should_expose_mcp_endpoint() | ||
| { | ||
| await Define<ScenarioContext>() | ||
| .Done(async _ => | ||
| { | ||
| var response = await InitializeMcpSession(); | ||
| return response.StatusCode == HttpStatusCode.OK; | ||
| }) | ||
| .Run(); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Should_list_audit_message_tools() | ||
| { | ||
| string toolsJson = null; | ||
|
|
||
| await Define<ScenarioContext>() | ||
| .Done(async _ => | ||
| { | ||
| var sessionId = await InitializeAndGetSessionId(); | ||
| if (sessionId == null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| var response = await SendMcpRequest(sessionId, "tools/list", new { }); | ||
| if (response == null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| toolsJson = await response.Content.ReadAsStringAsync(); | ||
| return response.StatusCode == HttpStatusCode.OK; | ||
| }) | ||
| .Run(); | ||
|
|
||
| Assert.That(toolsJson, Is.Not.Null); | ||
| var doc = JsonDocument.Parse(toolsJson); | ||
|
WilliamBZA marked this conversation as resolved.
Outdated
|
||
| var result = doc.RootElement.GetProperty("result"); | ||
| var tools = result.GetProperty("tools"); | ||
|
|
||
| var toolNames = tools.EnumerateArray() | ||
| .Select(t => t.GetProperty("name").GetString()) | ||
| .ToList(); | ||
|
|
||
| Assert.That(toolNames, Does.Contain("GetAuditMessages")); | ||
| Assert.That(toolNames, Does.Contain("SearchAuditMessages")); | ||
| Assert.That(toolNames, Does.Contain("GetAuditMessagesByEndpoint")); | ||
| Assert.That(toolNames, Does.Contain("GetAuditMessagesByConversation")); | ||
| Assert.That(toolNames, Does.Contain("GetAuditMessageBody")); | ||
| Assert.That(toolNames, Does.Contain("GetKnownEndpoints")); | ||
| Assert.That(toolNames, Does.Contain("GetEndpointAuditCounts")); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Should_call_get_audit_messages_tool() | ||
| { | ||
| string toolResult = null; | ||
|
|
||
| var context = await Define<MyContext>() | ||
| .WithEndpoint<Sender>(b => b.When((bus, c) => bus.Send(new MyMessage()))) | ||
| .WithEndpoint<Receiver>() | ||
| .Done(async c => | ||
| { | ||
| if (c.MessageId == null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // Wait for the message to be ingested | ||
| if (!await this.TryGetMany<MessagesView>("/api/messages?include_system_messages=false&sort=id", m => m.MessageId == c.MessageId)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| var sessionId = await InitializeAndGetSessionId(); | ||
| if (sessionId == null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| var response = await SendMcpRequest(sessionId, "tools/call", new | ||
| { | ||
| name = "GetAuditMessages", | ||
| arguments = new { includeSystemMessages = false, page = 1, perPage = 50 } | ||
| }); | ||
|
|
||
| if (response == null || response.StatusCode != HttpStatusCode.OK) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| toolResult = await response.Content.ReadAsStringAsync(); | ||
| return true; | ||
| }) | ||
| .Run(); | ||
|
|
||
| Assert.That(toolResult, Is.Not.Null); | ||
| var doc = JsonDocument.Parse(toolResult); | ||
| var result = doc.RootElement.GetProperty("result"); | ||
| var content = result.GetProperty("content"); | ||
| var textContent = content.EnumerateArray().First().GetProperty("text").GetString(); | ||
| var messagesResult = JsonDocument.Parse(textContent); | ||
| Assert.That(messagesResult.RootElement.GetProperty("totalCount").GetInt32(), Is.GreaterThanOrEqualTo(1)); | ||
| } | ||
|
|
||
| async Task<HttpResponseMessage> InitializeMcpSession() | ||
| { | ||
| var request = new HttpRequestMessage(HttpMethod.Post, "/mcp") | ||
| { | ||
| Content = JsonContent.Create(new | ||
| { | ||
| jsonrpc = "2.0", | ||
| id = 1, | ||
| method = "initialize", | ||
| @params = new | ||
| { | ||
| protocolVersion = "2025-03-26", | ||
| capabilities = new { }, | ||
| clientInfo = new { name = "test-client", version = "1.0" } | ||
| } | ||
| }) | ||
| }; | ||
| return await HttpClient.SendAsync(request); | ||
| } | ||
|
|
||
| async Task<string> InitializeAndGetSessionId() | ||
| { | ||
| var response = await InitializeMcpSession(); | ||
| if (response.StatusCode != HttpStatusCode.OK) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| if (response.Headers.TryGetValues("mcp-session-id", out var values)) | ||
| { | ||
| return values.FirstOrDefault(); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| async Task<HttpResponseMessage> SendMcpRequest(string sessionId, string method, object @params) | ||
| { | ||
| var request = new HttpRequestMessage(HttpMethod.Post, "/mcp") | ||
| { | ||
| Content = JsonContent.Create(new | ||
| { | ||
| jsonrpc = "2.0", | ||
| id = 2, | ||
| method, | ||
| @params | ||
| }) | ||
| }; | ||
| request.Headers.Add("mcp-session-id", sessionId); | ||
| return await HttpClient.SendAsync(request); | ||
| } | ||
|
|
||
| public class Sender : EndpointConfigurationBuilder | ||
| { | ||
| public Sender() => | ||
| EndpointSetup<DefaultServerWithoutAudit>(c => | ||
| { | ||
| var routing = c.ConfigureRouting(); | ||
| routing.RouteToEndpoint(typeof(MyMessage), typeof(Receiver)); | ||
| }); | ||
| } | ||
|
|
||
| public class Receiver : EndpointConfigurationBuilder | ||
| { | ||
| public Receiver() => EndpointSetup<DefaultServerWithAudit>(); | ||
|
|
||
| public class MyMessageHandler(MyContext testContext, IReadOnlySettings settings) : IHandleMessages<MyMessage> | ||
| { | ||
| public Task Handle(MyMessage message, IMessageHandlerContext context) | ||
| { | ||
| testContext.EndpointNameOfReceivingEndpoint = settings.EndpointName(); | ||
| testContext.MessageId = context.MessageId; | ||
| return Task.CompletedTask; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public class MyMessage : ICommand; | ||
|
|
||
| public class MyContext : ScenarioContext | ||
| { | ||
| public string MessageId { get; set; } | ||
| public string EndpointNameOfReceivingEndpoint { get; set; } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.