Skip to content

Commit c6c5333

Browse files
stephentoubCopilot
andcommitted
Add C# MCP OAuth config parity
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 07a4feb commit c6c5333

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

dotnet/src/Types.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,21 @@ public class AzureOptions
15261526
// MCP Server Configuration Types
15271527
// ============================================================================
15281528

1529+
/// <summary>
1530+
/// OAuth grant type for a remote MCP server.
1531+
/// </summary>
1532+
[JsonConverter(typeof(JsonStringEnumConverter<McpHttpServerConfigOauthGrantType>))]
1533+
public enum McpHttpServerConfigOauthGrantType
1534+
{
1535+
/// <summary>Use the authorization code OAuth flow.</summary>
1536+
[JsonStringEnumMemberName("authorization_code")]
1537+
AuthorizationCode,
1538+
1539+
/// <summary>Use the client credentials OAuth flow.</summary>
1540+
[JsonStringEnumMemberName("client_credentials")]
1541+
ClientCredentials
1542+
}
1543+
15291544
/// <summary>
15301545
/// Abstract base class for MCP server configurations.
15311546
/// </summary>
@@ -1611,6 +1626,24 @@ public sealed class McpHttpServerConfig : McpServerConfig
16111626
/// </summary>
16121627
[JsonPropertyName("headers")]
16131628
public IDictionary<string, string>? Headers { get; set; }
1629+
1630+
/// <summary>
1631+
/// Optional OAuth client ID for the remote server.
1632+
/// </summary>
1633+
[JsonPropertyName("oauthClientId")]
1634+
public string? OauthClientId { get; set; }
1635+
1636+
/// <summary>
1637+
/// Whether this is a public OAuth client.
1638+
/// </summary>
1639+
[JsonPropertyName("oauthPublicClient")]
1640+
public bool? OauthPublicClient { get; set; }
1641+
1642+
/// <summary>
1643+
/// Optional OAuth grant type for the remote server.
1644+
/// </summary>
1645+
[JsonPropertyName("oauthGrantType")]
1646+
public McpHttpServerConfigOauthGrantType? OauthGrantType { get; set; }
16141647
}
16151648

16161649
// ============================================================================

dotnet/test/SerializationTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,44 @@ public void SendMessageRequest_CanSerializeRequestHeaders_WithSdkOptions()
8181
Assert.Equal("trace-value", root.GetProperty("requestHeaders").GetProperty("X-Trace").GetString());
8282
}
8383

84+
[Fact]
85+
public void McpHttpServerConfig_CanSerializeOauthOptions_WithSdkOptions()
86+
{
87+
var options = GetSerializerOptions();
88+
McpServerConfig original = new McpHttpServerConfig
89+
{
90+
Url = "https://example.com/mcp",
91+
Headers = new Dictionary<string, string> { ["Authorization"] = "Bearer token" },
92+
OauthClientId = "client-id",
93+
OauthPublicClient = false,
94+
OauthGrantType = McpHttpServerConfigOauthGrantType.ClientCredentials,
95+
Tools = ["*"],
96+
Timeout = 3000
97+
};
98+
99+
var json = JsonSerializer.Serialize(original, options);
100+
using var document = JsonDocument.Parse(json);
101+
var root = document.RootElement;
102+
Assert.Equal("http", root.GetProperty("type").GetString());
103+
Assert.Equal("https://example.com/mcp", root.GetProperty("url").GetString());
104+
Assert.Equal("Bearer token", root.GetProperty("headers").GetProperty("Authorization").GetString());
105+
Assert.Equal("client-id", root.GetProperty("oauthClientId").GetString());
106+
Assert.False(root.GetProperty("oauthPublicClient").GetBoolean());
107+
Assert.Equal("client_credentials", root.GetProperty("oauthGrantType").GetString());
108+
Assert.Equal("*", root.GetProperty("tools")[0].GetString());
109+
Assert.Equal(3000, root.GetProperty("timeout").GetInt32());
110+
111+
var deserialized = JsonSerializer.Deserialize<McpServerConfig>(json, options);
112+
var httpConfig = Assert.IsType<McpHttpServerConfig>(deserialized);
113+
Assert.Equal("https://example.com/mcp", httpConfig.Url);
114+
Assert.Equal("Bearer token", httpConfig.Headers!["Authorization"]);
115+
Assert.Equal("client-id", httpConfig.OauthClientId);
116+
Assert.False(httpConfig.OauthPublicClient);
117+
Assert.Equal(McpHttpServerConfigOauthGrantType.ClientCredentials, httpConfig.OauthGrantType);
118+
Assert.Equal("*", Assert.Single(httpConfig.Tools));
119+
Assert.Equal(3000, httpConfig.Timeout);
120+
}
121+
84122
private static JsonSerializerOptions GetSerializerOptions()
85123
{
86124
var prop = typeof(CopilotClient)

0 commit comments

Comments
 (0)