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
10 changes: 10 additions & 0 deletions src/ModelContextProtocol.Core/Server/McpServerToolCreateOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ public sealed class McpServerToolCreateOptions
/// </value>
public JsonSerializerOptions? SerializerOptions { get; set; }

/// <summary>
/// Gets or sets a delegate used to marshal the result returned by the tool method.
/// </summary>
/// <remarks>
/// The delegate receives the result, its declared return type, and a cancellation token.
/// If <see langword="null"/>, the result is passed through unchanged.
/// </remarks>
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 +223,7 @@ internal McpServerToolCreateOptions Clone() =>
UseStructuredContent = UseStructuredContent,
OutputSchema = OutputSchema,
SerializerOptions = SerializerOptions,
MarshalResult = MarshalResult,
SchemaCreateOptions = SchemaCreateOptions,
Metadata = Metadata,
Icons = Icons,
Expand Down
23 changes: 23 additions & 0 deletions tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,29 @@ public async Task CanReturnString()
Assert.Equal("42", Assert.IsType<TextContentBlock>(result.Content[0]).Text);
}

[Fact]
public async Task SupportsCustomResultMarshaling()
{
Mock<McpServer> mockServer = new();
McpServerTool tool = McpServerTool.Create(() => 42, new()
{
MarshalResult = (result, resultType, cancellationToken) =>
{
Assert.Equal(42, result);
Assert.Equal(typeof(int), resultType);
Assert.Equal(TestContext.Current.CancellationToken, cancellationToken);
return new($"marshaled:{result}");
},
});

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

Assert.Single(result.Content);
Assert.Equal("marshaled:42", Assert.IsType<TextContentBlock>(result.Content[0]).Text);
}

[Fact]
public async Task CanReturnCollectionOfStrings()
{
Expand Down