Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions(
{
Name = options?.Name ?? method.GetCustomAttribute<McpServerToolAttribute>()?.Name ?? DeriveName(method),
Description = options?.Description,
MarshalResult = static (result, _, cancellationToken) => new ValueTask<object?>(result),
MarshalResult = options?.MarshalResult ?? (static (result, _, cancellationToken) => new ValueTask<object?>(result)),
SerializerOptions = options?.SerializerOptions ?? McpJsonUtilities.DefaultOptions,
JsonSchemaCreateOptions = options?.SchemaCreateOptions,
ConfigureParameterBinding = pi =>
Expand Down
11 changes: 11 additions & 0 deletions src/ModelContextProtocol.Core/Server/McpServerToolCreateOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ public sealed class McpServerToolCreateOptions
/// </value>
public JsonSerializerOptions? SerializerOptions { get; set; }

/// <summary>
/// Gets or sets a delegate used to determine the object returned from invoking the tool,
/// based on the result of the underlying method.
/// </summary>
/// <value>
/// The default is <see langword="null"/>, which means the underlying method's result is
/// returned as-is.
/// </value>
public Func<object?, Type?, CancellationToken, ValueTask<object?>>? MarshalResult { get; set; }

/// <summary>
/// Gets or sets the JSON schema options when creating an <see cref="AIFunction"/> from a method.
/// </summary>
Expand Down Expand Up @@ -214,6 +224,7 @@ internal McpServerToolCreateOptions Clone() =>
UseStructuredContent = UseStructuredContent,
OutputSchema = OutputSchema,
SerializerOptions = SerializerOptions,
MarshalResult = MarshalResult,
SchemaCreateOptions = SchemaCreateOptions,
Metadata = Metadata,
Icons = Icons,
Expand Down
39 changes: 39 additions & 0 deletions tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,45 @@ public async Task SupportsSchemaCreateOptions()
);
}

[Fact]
public async Task SupportsCustomResultMarshaling()
{
Type? seenDeclaredResultType = null;
Mock<McpServer> mockServer = new();
McpServerTool tool = McpServerTool.Create(() => 21 * 2, new()
{
Name = "marshaller",
MarshalResult = (result, declaredResultType, cancellationToken) =>
{
seenDeclaredResultType = declaredResultType;
Assert.False(cancellationToken.IsCancellationRequested);
return new ValueTask<object?>($"doubled:{result}");
},
});

var result = await tool.InvokeAsync(
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "marshaller" }),
TestContext.Current.CancellationToken);

Assert.Equal(typeof(int), seenDeclaredResultType);
var text = Assert.IsType<TextContentBlock>(Assert.Single(result.Content));
Assert.Equal("doubled:42", text.Text);
}

[Fact]
public async Task MarshalResult_Unset_ReturnsUnderlyingResultAsIs()
{
Mock<McpServer> mockServer = new();
McpServerTool tool = McpServerTool.Create(() => "42", new() { Name = "passthrough" });

var result = await tool.InvokeAsync(
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "passthrough" }),
TestContext.Current.CancellationToken);

var text = Assert.IsType<TextContentBlock>(Assert.Single(result.Content));
Assert.Equal("42", text.Text);
}

[Theory]
[MemberData(nameof(StructuredOutput_ReturnsExpectedSchema_Inputs))]
public async Task StructuredOutput_Enabled_ReturnsExpectedSchema<T>(T value)
Expand Down