Skip to content

Commit 0585c27

Browse files
authored
Fix form mode elicitation schema check to apply for all form requests (#1151)
1 parent eca882e commit 0585c27

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/ModelContextProtocol.Core/Server/McpServer.Methods.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ private void ThrowIfElicitationUnsupported(ElicitRequestParams request)
488488
throw new InvalidOperationException("Client does not support elicitation requests.");
489489
}
490490

491-
if (string.Equals(request.Mode, "form", StringComparison.Ordinal) && elicitationCapability.Form is null)
491+
if (string.Equals(request.Mode, "form", StringComparison.Ordinal))
492492
{
493493
if (request.RequestedSchema is null)
494494
{

tests/ModelContextProtocol.Tests/Protocol/UrlElicitationTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,26 @@ await request.Server.ElicitAsync(new()
163163
throw new McpException(ex.Message);
164164
}
165165
}
166+
else if (request.Params.Name == "TestFormElicitationMissingSchema")
167+
{
168+
try
169+
{
170+
await request.Server.ElicitAsync(new()
171+
{
172+
Message = "Form elicitation without schema should fail.",
173+
},
174+
cancellationToken);
175+
}
176+
catch (ArgumentException ex)
177+
{
178+
throw new McpException(ex.Message);
179+
}
180+
181+
return new CallToolResult
182+
{
183+
Content = [new TextContentBlock { Text = "missing-schema-succeeded" }],
184+
};
185+
}
166186

167187
Assert.Fail($"Unexpected tool name: {request.Params.Name}");
168188
return new CallToolResult { Content = [] };
@@ -506,6 +526,35 @@ public async Task UrlElicitationRequired_Exception_Propagates_To_Client()
506526
Assert.Equal("Authorization is required to access Example Co.", elicitation.Message);
507527
}
508528

529+
[Fact]
530+
public async Task FormElicitation_Requires_RequestedSchema()
531+
{
532+
var elicitationHandlerCalled = false;
533+
534+
await using McpClient client = await CreateMcpClientForServer(new McpClientOptions
535+
{
536+
Capabilities = new ClientCapabilities
537+
{
538+
Elicitation = new(),
539+
},
540+
Handlers = new McpClientHandlers()
541+
{
542+
ElicitationHandler = (request, cancellationToken) =>
543+
{
544+
elicitationHandlerCalled = true;
545+
return new ValueTask<ElicitResult>(new ElicitResult());
546+
},
547+
}
548+
});
549+
550+
var result = await client.CallToolAsync("TestFormElicitationMissingSchema", cancellationToken: TestContext.Current.CancellationToken);
551+
552+
Assert.True(result.IsError);
553+
var textContent = Assert.IsType<TextContentBlock>(result.Content[0]);
554+
Assert.Equal("An error occurred invoking 'TestFormElicitationMissingSchema': Form mode elicitation requests require a requested schema.", textContent.Text);
555+
Assert.False(elicitationHandlerCalled);
556+
}
557+
509558
private ElicitationCapability AssertServerElicitationCapability()
510559
{
511560
var capabilities = Server.ClientCapabilities;

0 commit comments

Comments
 (0)