Copilot Chat: agent request crashes with "Cannot read properties of undefined (reading 'type')" in countToolTokens when a tool input schema uses top-level JSON-Schema if/then (BYOK / custom model)
@vritant24 is already working on this.
Since Jun 26, 2026.
Assessment
This issue has not been assessed yet.
Description
Type: Bug
Summary
In agent mode with a custom / BYOK OpenAI-compatible model, a chat request aborts before sending whenever the active tool set includes an MCP tool whose input JSON Schema uses top-level if / then conditional keywords. The extension's local token counter (countToolTokens → countObjectTokens → countMessageObjectTokens → tokenLength) dereferences .type on a schema node it doesn't recognise and throws TypeError: Cannot read properties of undefined (reading 'type'). "Try Again" replays the same tool set and fails identically.
This only reproduces with custom/BYOK models, where the extension counts tool tokens locally. First-party (Copilot-hosted) models offload token counting and are unaffected.
Environment
- VS Code: 1.126.0 (commit 7e7950df89d055b5a378379db9ee14290772148a, 2026-06-23, stable)
- Bundled Copilot extension: 0.54.0
- OS: Linux (WSL2 remote)
- Model: a custom OpenAI-compatible endpoint registered via BYOK (provider type "custom OpenAI-compatible" / "custom endpoint")
Steps to reproduce
- Register a custom OpenAI-compatible model via BYOK and select it in agent mode.
- Register an MCP server that exposes a tool whose input schema uses top-level
if/then, e.g.:{ "name": "run_query", "inputSchema": { "type": "object", "properties": { "query": { "type": "string" }, "mode": { "type": "string" } }, "if": { "properties": { "mode": { "const": "nl" } } }, "then": { "required": ["query"] } } } - Send any agent-mode request with that tool in scope.
Actual behaviour
The extension first logs (repeatedly, per request):
[warning] Tool <tool-name> failed validation: object has unsupported top-level schema keyword 'if'
[warning] Tool <tool-name> failed validation: object has unsupported top-level schema keyword 'then'
…then the request aborts with:
TypeError: Cannot read properties of undefined (reading 'type')
at gre.tokenLength (<vscode-server>/bin/7e7950df.../extensions/copilot/dist/extension.js:1662:34964)
at gre.countMessageObjectTokens (...:1662:36231)
at gre.countMessageObjectTokens (...:1662:36504)
at process.processTicksAndRejections (node:internal/process/task_queues:104:5)
at async gre.countMessageObjectTokens (...:1662:36493)
at async gre.countObjectTokens (...:1662:36795)
at async gre.countToolTokens (...:1662:35927)
at async LG.countToolTokens (...:1690:16849)
at async LG._provideLanguageModelResponse (...:1690:11650)
at async LG.provideLanguageModelResponse (...:1690:15320)
Surfaced in the UI as: "Sorry, your request failed. Please try again."
Expected behaviour
Unsupported/unknown JSON-Schema keywords (if/then/else, etc.) should be ignored or stripped during token counting, not crash the request. tokenLength/countObjectTokens should be null-safe when walking schema nodes that have no type. At worst the tool should be skipped (as the validation warning implies), not abort the whole request.
Notes / workaround
- Deselecting the offending tool in the agent-mode tools picker avoids the crash.
- Top-level
if/thenis valid JSON Schema (Draft-07+); the failure is in how the token counter handles a schema node it chose not to support.
Impact
Any third-party MCP tool that ships a Draft-07 conditional schema makes custom/BYOK models unusable whenever that tool is in scope. Client request IDs from affected sessions available on request for telemetry correlation.
- Dominant language
- TypeScript
- Stars
- 193k
- Forks
- 42.9k
- PR merge metrics
- PR metrics pending
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from microsoft/vscode
-
testplan-item
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
new release
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
testplan-item
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
testplan-item
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
All issues in microsoft/vscode
Similar issues
-
clawsweeper:fix-shape-clear clawsweeper:queueable-fix clawsweeper:source-repro impact:ux-friction issue-rating: 🦞 diamond lobster no-stale P3
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
community first-timers-only good first issue hacktoberfest help wanted low hanging fruit up-for-grabs
Difficulty 1/5 Under an hour Newbie friendliness 76/100
-
code-quality refactoring
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
github/gh-aw-firewall#8816 ·
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
langchain-ai/deepagents#6450 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
vercel/react-tweet#225 ·