-
Notifications
You must be signed in to change notification settings - Fork 50
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
24
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 all commits
Commits
Show all changes
24 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 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
427 changes: 427 additions & 0 deletions
427
.../ApprovalFiles/When_mcp_server_is_enabled.Should_list_primary_instance_tools.approved.txt
Large diffs are not rendered by default.
Oops, something went wrong.
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
151 changes: 151 additions & 0 deletions
151
src/ServiceControl.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,151 @@ | ||
| namespace ServiceControl.AcceptanceTests.Mcp; | ||
|
|
||
| using System.Collections.Generic; | ||
| 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 NServiceBus.AcceptanceTesting; | ||
| using NUnit.Framework; | ||
| using Particular.Approvals; | ||
|
|
||
| [TestFixture] | ||
| 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_primary_instance_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 ReadMcpResponseJson(response); | ||
| return response.StatusCode == HttpStatusCode.OK; | ||
| }) | ||
| .Run(); | ||
|
|
||
| Assert.That(toolsJson, Is.Not.Null); | ||
| var mcpResponse = JsonSerializer.Deserialize<McpListToolsResponse>(toolsJson, JsonOptions)!; | ||
| var sortedTools = mcpResponse.Result.Tools.Cast<JsonElement>().OrderBy(t => t.GetProperty("name").GetString()).ToList(); | ||
| var formattedTools = JsonSerializer.Serialize(sortedTools, new JsonSerializerOptions { WriteIndented = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); | ||
| Approver.Verify(formattedTools); | ||
| } | ||
|
|
||
| static readonly JsonSerializerOptions JsonOptions = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; | ||
|
|
||
| class McpListToolsResponse | ||
| { | ||
| public McpListToolsResult Result { get; set; } | ||
| } | ||
|
|
||
| class McpListToolsResult | ||
| { | ||
| public List<object> Tools { get; set; } = []; | ||
| } | ||
|
|
||
| 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" } | ||
| } | ||
| }) | ||
| }; | ||
| request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); | ||
| request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("text/event-stream")); | ||
| 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; | ||
| } | ||
|
|
||
| static async Task<string> ReadMcpResponseJson(HttpResponseMessage response) | ||
| { | ||
| var body = await response.Content.ReadAsStringAsync(); | ||
| var contentType = response.Content.Headers.ContentType?.MediaType; | ||
|
|
||
| if (contentType == "text/event-stream") | ||
| { | ||
| foreach (var line in body.Split('\n')) | ||
| { | ||
| if (line.StartsWith("data: ")) | ||
| { | ||
| return line.Substring("data: ".Length); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return body; | ||
| } | ||
|
|
||
| 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.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); | ||
| request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("text/event-stream")); | ||
| request.Headers.Add("mcp-session-id", sessionId); | ||
| return await HttpClient.SendAsync(request); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we better off passing the settings object in at this point?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... Maybe yeah but I don't know what a reasonable threshold would be to make that flip but I agree it starts to smell