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: 1 addition & 1 deletion samples/AspNetCoreSseServer/Tools/SampleLlmTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace TestServerWithHosting.Tools;
[McpServerToolType]
public static class SampleLlmTool
{
[McpServerTool("sampleLLM"), Description("Samples from an LLM using MCP's sampling feature")]
[McpServerTool(Name = "sampleLLM"), Description("Samples from an LLM using MCP's sampling feature")]
public static async Task<string> SampleLLM(
IMcpServer thisServer,
[Description("The prompt to send to the LLM")] string prompt,
Expand Down
2 changes: 1 addition & 1 deletion samples/TestServerWithHosting/Tools/SampleLlmTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace TestServerWithHosting.Tools;
[McpServerToolType]
public static class SampleLlmTool
{
[McpServerTool("sampleLLM"), Description("Samples from an LLM using MCP's sampling feature")]
[McpServerTool(Name = "sampleLLM"), Description("Samples from an LLM using MCP's sampling feature")]
public static async Task<string> SampleLLM(
IMcpServer thisServer,
[Description("The prompt to send to the LLM")] string prompt,
Expand Down
4 changes: 3 additions & 1 deletion src/ModelContextProtocol/AIContentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static AIContent ToAIContent(this Content content)
Throw.IfNull(content);

AIContent ac;
if (content is { Type: "image", MimeType: not null, Data: not null })
if (content is { Type: "image" or "audio", MimeType: not null, Data: not null })
{
ac = new DataContent(Convert.FromBase64String(content.Data), content.MimeType);
}
Expand Down Expand Up @@ -112,6 +112,7 @@ internal static Content ToContent(this AIContent content) =>
Text = textContent.Text,
Type = "text",
},

DataContent dataContent => new()
{
Data = dataContent.GetBase64Data(),
Expand All @@ -121,6 +122,7 @@ internal static Content ToContent(this AIContent content) =>
dataContent.HasTopLevelMediaType("audio") ? "audio" :
"resource",
},

_ => new()
{
Text = JsonSerializer.Serialize(content, McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(object))),
Expand Down
6 changes: 3 additions & 3 deletions src/ModelContextProtocol/Client/McpClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ internal static (IList<ChatMessage> Messages, ChatOptions? Options) ToChatClient
{
message.Contents.Add(new TextContent(sm.Content.Text));
}
else if (sm.Content is { Type: "image", MimeType: not null, Data: not null })
else if (sm.Content is { Type: "image" or "audio", MimeType: not null, Data: not null })
{
message.Contents.Add(new DataContent(Convert.FromBase64String(sm.Content.Data), sm.Content.MimeType));
}
Expand Down Expand Up @@ -512,11 +512,11 @@ internal static CreateMessageResult ToCreateMessageResult(this ChatResponse chat
{
foreach (var lmc in lastMessage.Contents)
{
if (lmc is DataContent dc && dc.HasTopLevelMediaType("image"))
if (lmc is DataContent dc && (dc.HasTopLevelMediaType("image") || dc.HasTopLevelMediaType("audio")))
{
content = new()
{
Type = "image",
Type = dc.HasTopLevelMediaType("image") ? "image" : "audio",
MimeType = dc.MediaType,
Data = dc.GetBase64Data(),
};
Expand Down
14 changes: 8 additions & 6 deletions src/ModelContextProtocol/Client/McpClientTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@ namespace ModelContextProtocol.Client;
public sealed class McpClientTool : AIFunction
{
private readonly IMcpClient _client;
private readonly Tool _tool;

internal McpClientTool(IMcpClient client, Tool tool)
{
_client = client;
_tool = tool;
ProtocolTool = tool;
}

/// <summary>Gets the protocol <see cref="Tool"/> type for this instance.</summary>
public Tool ProtocolTool { get; }

/// <inheritdoc/>
public override string Name => _tool.Name;
public override string Name => ProtocolTool.Name;

/// <inheritdoc/>
public override string Description => _tool.Description ?? string.Empty;
public override string Description => ProtocolTool.Description ?? string.Empty;

/// <inheritdoc/>
public override JsonElement JsonSchema => _tool.InputSchema;
public override JsonElement JsonSchema => ProtocolTool.InputSchema;

/// <inheritdoc/>
public override JsonSerializerOptions JsonSerializerOptions => McpJsonUtilities.DefaultOptions;
Expand All @@ -37,7 +39,7 @@ internal McpClientTool(IMcpClient client, Tool tool)
arguments as IReadOnlyDictionary<string, object?> ??
arguments.ToDictionary();

CallToolResponse result = await _client.CallToolAsync(_tool.Name, argDict, cancellationToken).ConfigureAwait(false);
CallToolResponse result = await _client.CallToolAsync(ProtocolTool.Name, argDict, cancellationToken).ConfigureAwait(false);
return JsonSerializer.SerializeToElement(result, McpJsonUtilities.JsonContext.Default.CallToolResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,9 @@ public static partial class McpServerBuilderExtensions
{
if (toolMethod.GetCustomAttribute<McpServerToolAttribute>() is not null)
{
if (toolMethod.IsStatic)
{
builder.Services.AddSingleton(services => McpServerTool.Create(toolMethod, services: services));
}
else
{
builder.Services.AddSingleton(services => McpServerTool.Create(toolMethod, typeof(TTool), services: services));
}
builder.Services.AddSingleton((Func<IServiceProvider, McpServerTool>)(toolMethod.IsStatic ?
services => McpServerTool.Create(toolMethod, new McpServerToolCreateOptions() { Services = services }) :
services => McpServerTool.Create(toolMethod, typeof(TTool), new() { Services = services })));
}
}

Expand All @@ -71,18 +66,13 @@ public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, params
{
if (toolType is not null)
{
foreach (var method in toolType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance))
foreach (var toolMethod in toolType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance))
{
if (method.GetCustomAttribute<McpServerToolAttribute>() is not null)
if (toolMethod.GetCustomAttribute<McpServerToolAttribute>() is not null)
{
if (method.IsStatic)
{
builder.Services.AddSingleton(services => McpServerTool.Create(method, services: services));
}
else
{
builder.Services.AddSingleton(services => McpServerTool.Create(method, toolType, services: services));
}
builder.Services.AddSingleton((Func<IServiceProvider, McpServerTool>)(toolMethod.IsStatic ?
services => McpServerTool.Create(toolMethod, new McpServerToolCreateOptions() { Services = services }) :
services => McpServerTool.Create(toolMethod, toolType, new() { Services = services })));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public sealed class StdioServerTransport : TransportBase, IServerTransport
private readonly TextReader _stdInReader;
private readonly Stream _stdOutStream;

private SemaphoreSlim _sendLock = new(1, 1);
private readonly SemaphoreSlim _sendLock = new(1, 1);
private Task? _readTask;
private CancellationTokenSource? _shutdownCts;

Expand Down
2 changes: 1 addition & 1 deletion src/ModelContextProtocol/Protocol/Types/Annotated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace ModelContextProtocol.Protocol.Types;

/// <summary>
/// Base for objects that include optional annotations for the client. The client can use annotations to inform how objects are used or displayed.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
public abstract record Annotated
{
Expand Down
2 changes: 1 addition & 1 deletion src/ModelContextProtocol/Protocol/Types/Annotations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace ModelContextProtocol.Protocol.Types;

/// <summary>
/// Represents annotations that can be attached to content.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
public record Annotations
{
Expand Down
2 changes: 1 addition & 1 deletion src/ModelContextProtocol/Protocol/Types/Argument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace ModelContextProtocol.Protocol.Types;

/// <summary>
/// Used for completion requests to provide additional context for the completion options.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
public class Argument
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/// <summary>
/// Used by the client to invoke a tool provided by the server.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
public class CallToolRequestParams : RequestParams
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/// However, any errors in _finding_ the tool, an error indicating that the
/// server does not support tool calls, or any other exceptional conditions,
/// should be reported as an MCP error response.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
public class CallToolResponse
{
Expand Down
14 changes: 7 additions & 7 deletions src/ModelContextProtocol/Protocol/Types/Capabilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace ModelContextProtocol.Protocol.Types;

/// <summary>
/// Represents the capabilities that a client may support.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
public class ClientCapabilities
{
Expand All @@ -30,7 +30,7 @@ public class ClientCapabilities

/// <summary>
/// Represents the roots capability configuration.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
public class RootsCapability
{
Expand All @@ -47,7 +47,7 @@ public class RootsCapability

/// <summary>
/// Represents the sampling capability configuration.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
public class SamplingCapability
{
Expand All @@ -60,7 +60,7 @@ public class SamplingCapability

/// <summary>
/// Represents the logging capability configuration.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
public class LoggingCapability
{
Expand All @@ -76,7 +76,7 @@ public class LoggingCapability

/// <summary>
/// Represents the prompts capability configuration.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
public class PromptsCapability
{
Expand All @@ -101,7 +101,7 @@ public class PromptsCapability

/// <summary>
/// Represents the resources capability configuration.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
public class ResourcesCapability
{
Expand Down Expand Up @@ -150,7 +150,7 @@ public class ResourcesCapability

/// <summary>
/// Represents the tools capability configuration.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
public class ToolsCapability
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/// <summary>
/// A request from the client to the server, to ask for completion options.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
public class CompleteRequestParams : RequestParams
{
Expand Down
2 changes: 1 addition & 1 deletion src/ModelContextProtocol/Protocol/Types/CompleteResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace ModelContextProtocol.Protocol.Types;

/// <summary>
/// The server's response to a completion/complete request
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
public class CompleteResult
{
Expand Down
2 changes: 1 addition & 1 deletion src/ModelContextProtocol/Protocol/Types/Completion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace ModelContextProtocol.Protocol.Types;

/// <summary>
/// Represents a completion object in the server's response
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
public class Completion
{
Expand Down
4 changes: 2 additions & 2 deletions src/ModelContextProtocol/Protocol/Types/Content.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ namespace ModelContextProtocol.Protocol.Types;

/// <summary>
/// Represents the content of a tool response.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// There are multiple subtypes of content, depending on the "type" field, these are represented as separate classes.
/// </summary>
public class Content
{
/// <summary>
/// The type of content. This determines the structure of the content object. Can be "image", "text", "resource".
/// The type of content. This determines the structure of the content object. Can be "image", "audio", "text", "resource".
/// </summary>

[JsonPropertyName("type")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace ModelContextProtocol.Protocol.Types;

/// <summary>
/// A request to include context from one or more MCP servers (including the caller), to be attached to the prompt.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
[JsonConverter(typeof(JsonStringEnumConverter<ContextInclusion>))]
public enum ContextInclusion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
///
/// While these align with the protocol specification,
/// clients have full discretion over model selection and should inform users before sampling.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
public class CreateMessageRequestParams : RequestParams
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace ModelContextProtocol.Protocol.Types;
/// The client's response to a sampling/create_message request from the server.
/// The client should inform the user before returning the sampled message, to allow them to inspect the response (human in the loop)
/// and decide whether to allow the server to see it.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
public class CreateMessageResult
{
Expand Down
2 changes: 1 addition & 1 deletion src/ModelContextProtocol/Protocol/Types/EmptyResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/// <summary>
/// An empty result object.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
public class EmptyResult
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/// <summary>
/// Used by the client to get a prompt provided by the server.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
public class GetPromptRequestParams : RequestParams
{
Expand Down
2 changes: 1 addition & 1 deletion src/ModelContextProtocol/Protocol/Types/GetPromptResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/// <summary>
/// The server's response to a prompts/get request from the client.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
public class GetPromptResult
{
Expand Down
2 changes: 1 addition & 1 deletion src/ModelContextProtocol/Protocol/Types/Implementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace ModelContextProtocol.Protocol.Types;

/// <summary>
/// Describes the name and version of an MCP implementation.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
public class Implementation
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace ModelContextProtocol.Protocol.Types;

/// <summary>
/// Parameters for an initialization request sent to the server.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
public class InitializeRequestParams : RequestParams
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace ModelContextProtocol.Protocol.Types;

/// <summary>
/// Result of the initialization request sent to the server.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
public record InitializeResult
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/// <summary>
/// Sent from the client to request a list of prompts and prompt templates the server has.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
/// </summary>
public class ListPromptsRequestParams
{
Expand Down
Loading