Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="$(MicrosoftExtensionsVersion)" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageVersion Include="Moq" Version="4.20.72" />
<PackageVersion Include="OpenTelemetry" Version="1.11.2" />
<PackageVersion Include="OpenTelemetry.Exporter.InMemory" Version="1.11.2" />
<PackageVersion Include="Serilog.Extensions.Hosting" Version="9.0.0" />
<PackageVersion Include="Serilog.Extensions.Logging" Version="9.0.0" />
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0" />
Expand Down
29 changes: 29 additions & 0 deletions src/ModelContextProtocol/Diagnostics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Diagnostics;
using System.Diagnostics.Metrics;

namespace ModelContextProtocol;

internal static class Diagnostics
{
internal static ActivitySource ActivitySource { get; } = new("ModelContextProtocol");

/// <summary>
/// Follows boundaries from http.server.request.duration/http.client.request.duration
/// </summary>
internal static InstrumentAdvice<double> ShortSecondsBucketBoundaries { get; } = new()
{
HistogramBucketBoundaries = [0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10],
};

/// <summary>
/// Not based on a standard. Larger bucket sizes for longer lasting operations, e.g. HTTP connection duration.
/// See https://github.com/open-telemetry/semantic-conventions/issues/336
/// </summary>
internal static InstrumentAdvice<double> LongSecondsBucketBoundaries { get; } = new()
{
HistogramBucketBoundaries = [0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 30, 60, 120, 300],
};

internal static Meter Meter { get; } = new("ModelContextProtocol");

}
23 changes: 12 additions & 11 deletions src/ModelContextProtocol/Protocol/Messages/ProgressToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,45 @@ namespace ModelContextProtocol.Protocol.Messages;
[JsonConverter(typeof(Converter))]
public readonly struct ProgressToken : IEquatable<ProgressToken>
{
/// <summary>The id, either a string or a boxed long or null.</summary>
private readonly object? _id;
/// <summary>The token, either a string or a boxed long or null.</summary>
private readonly object? _token;

/// <summary>Initializes a new instance of the <see cref="ProgressToken"/> with a specified value.</summary>
/// <param name="value">The required ID value.</param>
public ProgressToken(string value)
{
Throw.IfNull(value);
_id = value;
_token = value;
}

/// <summary>Initializes a new instance of the <see cref="ProgressToken"/> with a specified value.</summary>
/// <param name="value">The required ID value.</param>
public ProgressToken(long value)
{
// Box the long. Progress tokens are almost always strings in practice, so this should be rare.
_id = value;
_token = value;
}

/// <summary>Gets whether the identifier is uninitialized.</summary>
public bool IsDefault => _id is null;
/// <summary>Gets the underlying object for this token.</summary>
/// <remarks>This will either be a <see cref="string"/>, a boxed <see cref="long"/>, or <see langword="null"/>.</remarks>
public object? Token => _token;

/// <inheritdoc />
public override string? ToString() =>
_id is string stringValue ? $"\"{stringValue}\"" :
_id is long longValue ? longValue.ToString(CultureInfo.InvariantCulture) :
_token is string stringValue ? $"{stringValue}" :
_token is long longValue ? longValue.ToString(CultureInfo.InvariantCulture) :
null;

/// <summary>
/// Compares this ProgressToken to another ProgressToken.
/// </summary>
public bool Equals(ProgressToken other) => Equals(_id, other._id);
public bool Equals(ProgressToken other) => Equals(_token, other._token);

/// <inheritdoc />
public override bool Equals(object? obj) => obj is ProgressToken other && Equals(other);

/// <inheritdoc />
public override int GetHashCode() => _id?.GetHashCode() ?? 0;
public override int GetHashCode() => _token?.GetHashCode() ?? 0;

/// <summary>
/// Compares two ProgressTokens for equality.
Expand Down Expand Up @@ -83,7 +84,7 @@ public override void Write(Utf8JsonWriter writer, ProgressToken value, JsonSeria
{
Throw.IfNull(writer);

switch (value._id)
switch (value._token)
{
case string str:
writer.WriteStringValue(str);
Expand Down
7 changes: 4 additions & 3 deletions src/ModelContextProtocol/Protocol/Messages/RequestId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ public RequestId(long value)
_id = value;
}

/// <summary>Gets whether the identifier is uninitialized.</summary>
public bool IsDefault => _id is null;
/// <summary>Gets the underlying object for this id.</summary>
/// <remarks>This will either be a <see cref="string"/>, a boxed <see cref="long"/>, or <see langword="null"/>.</remarks>
public object? Id => _id;

/// <inheritdoc />
public override string ToString() =>
_id is string stringValue ? $"\"{stringValue}\"" :
_id is string stringValue ? stringValue :
_id is long longValue ? longValue.ToString(CultureInfo.InvariantCulture) :
string.Empty;

Expand Down
3 changes: 2 additions & 1 deletion src/ModelContextProtocol/Shared/McpJsonRpcEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using ModelContextProtocol.Logging;
using ModelContextProtocol.Protocol.Messages;
using ModelContextProtocol.Protocol.Transport;
using ModelContextProtocol.Server;
using ModelContextProtocol.Utils;
using System.Diagnostics.CodeAnalysis;

Expand Down Expand Up @@ -63,7 +64,7 @@ public Task SendMessageAsync(IJsonRpcMessage message, CancellationToken cancella
protected void StartSession(ITransport sessionTransport)
{
_sessionCts = new CancellationTokenSource();
_session = new McpSession(sessionTransport, EndpointName, _requestHandlers, _notificationHandlers, _logger);
_session = new McpSession(this is IMcpServer, sessionTransport, EndpointName, _requestHandlers, _notificationHandlers, _logger);
MessageProcessingTask = _session.ProcessMessagesAsync(_sessionCts.Token);
}

Expand Down
Loading