Skip to content

Commit e879595

Browse files
Copilotstephentoub
andcommitted
Add test validating ArgumentException for form mode elicitation without schema
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent e8aa1fe commit e879595

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

tests/ModelContextProtocol.Tests/Protocol/UrlElicitationTests.cs

Lines changed: 52 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,38 @@ 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 ElicitationCapability
539+
{
540+
Url = new UrlElicitationCapability(),
541+
}
542+
},
543+
Handlers = new McpClientHandlers()
544+
{
545+
ElicitationHandler = (request, cancellationToken) =>
546+
{
547+
elicitationHandlerCalled = true;
548+
return new ValueTask<ElicitResult>(new ElicitResult());
549+
},
550+
}
551+
});
552+
553+
var result = await client.CallToolAsync("TestFormElicitationMissingSchema", cancellationToken: TestContext.Current.CancellationToken);
554+
555+
Assert.True(result.IsError);
556+
var textContent = Assert.IsType<TextContentBlock>(result.Content[0]);
557+
Assert.Equal("An error occurred invoking 'TestFormElicitationMissingSchema': Form mode elicitation requests require a requested schema.", textContent.Text);
558+
Assert.False(elicitationHandlerCalled);
559+
}
560+
509561
private ElicitationCapability AssertServerElicitationCapability()
510562
{
511563
var capabilities = Server.ClientCapabilities;

0 commit comments

Comments
 (0)