chat: use structured outputs for SearchBookContent MCP tool#1079
Draft
BenjaminMichaelis wants to merge 1 commit intomainfrom
Draft
chat: use structured outputs for SearchBookContent MCP tool#1079BenjaminMichaelis wants to merge 1 commit intomainfrom
BenjaminMichaelis wants to merge 1 commit intomainfrom
Conversation
- Convert SearchBookContent from Task<string> to Task<CallToolResult> with UseStructuredContent = true and OutputSchemaType = typeof(SearchBookContentResult), matching the pattern established by FindBookHelpForDiagnostic and SearchListingsByCode - Add SearchBookContentMatchResult and SearchBookContentResult records to McpToolResults.cs - Fix no-results path to return CreateHybridResult with empty Matches list instead of CreateError — zero search results is a valid outcome, not a tool failure - Add explicit OutputSchemaType to GetChapterList, GetChapterSections, GetDirectContentUrl, GetNavigationContext, and GetChapterSummary for documentation clarity (MCP SDK already infers the output schema from the concrete return type, but explicit annotation makes the intended schema contract visible at the declaration site) - Add search_book_content to McpToolContractTests outputSchema presence assertions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Improves structured output consistency across the AI chat RAG pipeline by converting
SearchBookContent— the primary semantic vector search tool — to use the same hybrid structured content pattern already established byFindBookHelpForDiagnosticandSearchListingsByCode.The changes were reviewed and approved by Claude Opus 4.6 and GPT-5.5 subagents, iterating to agreement.
Changes
SearchBookContentconverted to structured outputSearchBookContentwas the only major RAG tool still returning plain text. It now returns aCallToolResultwith:UseStructuredContent = trueandOutputSchemaType = typeof(SearchBookContentResult)McpToolResultFormatter.GetModelInput()will now return the JSONStructuredContentto the model instead of the formatted text, consistent with all other structured toolsContentblock for MCP clients that display it"No results" semantics fixed
The zero-results path previously returned
McpToolResultFactory.CreateError()(IsError = true). Zero results from a search query is a valid outcome, not a tool failure. It now returnsCreateHybridResultwith an emptyMatches: []list, so the model always receives well-typed JSON conforming to the schema.Validation failures (empty query, query too long, AI service not configured) remain correctly modeled as errors.
New structured result types
Added to
McpToolResults.cs:SearchBookContentMatchResult— one vector search hit (score, chapter number, heading, text chunk)SearchBookContentResult— wrapper withMatcheslist; this is the advertised output schemaOutputSchemaTypemade explicit on allUseStructuredContenttoolsFive tools that had
UseStructuredContent = truebut noOutputSchemaTypewere updated:GetChapterList,GetChapterSections,GetDirectContentUrl,GetNavigationContext,GetChapterSummary.The MCP SDK already auto-infers the output schema from the concrete return type (confirmed by pre-existing integration tests), so this is redundant but makes the intended schema contract explicit at the declaration site — consistent with how
FindBookHelpForDiagnosticandSearchListingsByCodeare declared.Contract test updated
McpToolContractTests.McpToolsList_StructuredAndHybridTools_AdvertiseOutputSchemanow asserts thatsearch_book_contentadvertises anoutputSchema, alongside the other structured tools.What was NOT changed (and why)
ResponseTextFormat.CreateJsonSchemaFormat()does not exist in OpenAI SDK 2.7.0 for the Responses API. Schema-enforced output on the streaming text response isn't possible without switching to the Chat Completions API, and would break markdown rendering anyway. Both subagents agreed this is correct.strictModeEnabled: trueon tool input registration — Already correct inAIChatService.cs, untouched.LookupConcept,CheckTopicCoverage,FindRelatedSections, etc.) — These return prose-heavy navigation summaries where structured content wouldn't add value for the model.Testing
search_book_contentschema advertisement