Commit 73868a6
authored
chat: use structured outputs for SearchBookContent MCP tool (#1079)
## 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 by `FindBookHelpForDiagnostic` and `SearchListingsByCode`.
## Changes
### `SearchBookContent` converted to structured output
`SearchBookContent` was the only major RAG tool still returning plain
text. It now returns a `CallToolResult` with:
- `UseStructuredContent = true` and `OutputSchemaType =
typeof(SearchBookContentResult)`
- `McpToolResultFormatter.GetModelInput()` will now return the JSON
`StructuredContent` to the model instead of the formatted text,
consistent with all other structured tools
- The human-readable text is preserved as the first `Content` block 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
returns `CreateHybridResult` with an empty `Matches: []` 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 with `Matches` list; this is the
advertised output schema
### `OutputSchemaType` made explicit on all `UseStructuredContent` tools
Five tools that had `UseStructuredContent = true` but no
`OutputSchemaType` were 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 `FindBookHelpForDiagnostic` and
`SearchListingsByCode` are declared.
### Contract test updated
`McpToolContractTests.McpToolsList_StructuredAndHybridTools_AdvertiseOutputSchema`
now asserts that `search_book_content` advertises an `outputSchema`,
alongside the other structured tools.
## What was NOT changed (and why)
- **Main chat response format** —
`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: true` on tool input registration** — Already
correct in `AIChatService.cs`, untouched.
- **Plain-text tools** (`LookupConcept`, `CheckTopicCoverage`,
`FindRelatedSections`, etc.) — These return prose-heavy navigation
summaries where structured content wouldn't add value for the model.
## Testing
- ✅ Solution builds: 0 errors, 0 warnings
- ✅ All 11 chat unit tests pass
- ✅ Contract test updated to cover `search_book_content` schema
advertisement1 parent aea80c9 commit 73868a6
4 files changed
Lines changed: 44 additions & 19 deletions
File tree
- EssentialCSharp.Web.Tests
- EssentialCSharp.Web
- Models
- Tools
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| 40 | + | |
40 | 41 | | |
41 | 42 | | |
42 | 43 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
3 | 12 | | |
4 | 13 | | |
5 | 14 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
136 | 136 | | |
137 | 137 | | |
138 | 138 | | |
139 | | - | |
| 139 | + | |
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
143 | 143 | | |
144 | 144 | | |
145 | | - | |
| 145 | + | |
146 | 146 | | |
147 | 147 | | |
148 | 148 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
| 32 | + | |
33 | 33 | | |
34 | | - | |
| 34 | + | |
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | | - | |
| 41 | + | |
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | | - | |
| 45 | + | |
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | | - | |
| 50 | + | |
51 | 51 | | |
52 | 52 | | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
57 | 66 | | |
58 | 67 | | |
59 | 68 | | |
60 | 69 | | |
61 | 70 | | |
62 | 71 | | |
63 | 72 | | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
68 | 81 | | |
69 | | - | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
70 | 85 | | |
71 | 86 | | |
72 | | - | |
| 87 | + | |
73 | 88 | | |
74 | 89 | | |
75 | 90 | | |
76 | | - | |
| 91 | + | |
77 | 92 | | |
78 | 93 | | |
79 | 94 | | |
80 | 95 | | |
81 | 96 | | |
82 | | - | |
| 97 | + | |
83 | 98 | | |
84 | 99 | | |
85 | 100 | | |
| |||
0 commit comments