-
Notifications
You must be signed in to change notification settings - Fork 5
Implement IOptionsFormatter on McpOptions for improved serviceability #111
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,16 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Azure.WebJobs.Hosting; | ||
| using Newtonsoft.Json.Linq; | ||
| using System.ComponentModel; | ||
|
|
||
| namespace Microsoft.Azure.Functions.Extensions.Mcp.Configuration; | ||
|
|
||
| /// <summary> | ||
| /// Options for configuring the Model Context Protocol (MCP) extension in Azure Functions. | ||
| /// </summary> | ||
| public sealed class McpOptions | ||
| public sealed class McpOptions : IOptionsFormatter | ||
| { | ||
| private MessageOptions _messageOptions = new(); | ||
|
|
||
|
|
@@ -41,4 +45,20 @@ public MessageOptions MessageOptions | |
| set => _messageOptions = value | ||
| ?? throw new ArgumentNullException(nameof(value),"Message options cannot be null."); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| [EditorBrowsable(EditorBrowsableState.Never)] | ||
| string IOptionsFormatter.Format() | ||
| { | ||
| JObject options = new JObject | ||
| { | ||
| { nameof(ServerName), ServerName }, | ||
| { nameof(ServerVersion), ServerVersion }, | ||
| { nameof(Instructions), Instructions }, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's omit Instructions, since this may contain customer sensitive info |
||
| { nameof(EncryptClientState), EncryptClientState }, | ||
| { nameof(MessageOptions) + "." + nameof(MessageOptions.UseAbsoluteUriForEndpoint), MessageOptions.UseAbsoluteUriForEndpoint } | ||
| }; | ||
|
|
||
| return options.ToString(Newtonsoft.Json.Formatting.Indented); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Azure.Functions.Extensions.Mcp.Configuration; | ||
| using Microsoft.Azure.WebJobs.Hosting; | ||
| using Newtonsoft.Json.Linq; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Azure.Functions.Extensions.Mcp.Tests; | ||
|
|
@@ -20,4 +22,45 @@ public void DefaultValues_AreSetCorrectly() | |
| Assert.NotNull(options.MessageOptions); | ||
| Assert.False(options.MessageOptions.UseAbsoluteUriForEndpoint); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Format_ReturnsExpectedJson() | ||
| { | ||
| var options = new McpOptions | ||
| { | ||
| ServerName = "Test Server", | ||
| ServerVersion = "2.0.0", | ||
| Instructions = "Test instructions", | ||
| EncryptClientState = false | ||
| }; | ||
| options.MessageOptions.UseAbsoluteUriForEndpoint = true; | ||
|
|
||
| var formatter = (IOptionsFormatter)options; | ||
| var formatted = formatter.Format(); | ||
|
|
||
| Assert.NotNull(formatted); | ||
|
|
||
| var json = JObject.Parse(formatted); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also do an assertion on the number of properties to make the test stronger |
||
| Assert.Equal("Test Server", json[nameof(options.ServerName)]?.ToString()); | ||
| Assert.Equal("2.0.0", json[nameof(options.ServerVersion)]?.ToString()); | ||
| Assert.Equal("Test instructions", json[nameof(options.Instructions)]?.ToString()); | ||
| Assert.Equal("False", json[nameof(options.EncryptClientState)]?.ToString()); | ||
| Assert.Equal("True", json["MessageOptions.UseAbsoluteUriForEndpoint"]?.ToString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Format_IncludesAllNonSensitiveProperties() | ||
| { | ||
| var options = new McpOptions(); | ||
| var formatter = (IOptionsFormatter)options; | ||
| var formatted = formatter.Format(); | ||
|
|
||
| var json = JObject.Parse(formatted); | ||
|
|
||
| Assert.Contains(nameof(options.ServerName), json.Properties().Select(p => p.Name)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here - do an assertion on the number of properties logged |
||
| Assert.Contains(nameof(options.ServerVersion), json.Properties().Select(p => p.Name)); | ||
| Assert.Contains(nameof(options.Instructions), json.Properties().Select(p => p.Name)); | ||
| Assert.Contains(nameof(options.EncryptClientState), json.Properties().Select(p => p.Name)); | ||
| Assert.Contains("MessageOptions.UseAbsoluteUriForEndpoint", json.Properties().Select(p => p.Name)); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
Use System.Text.Json rather than Newtonsoft