UriTemplate.match() throws a TypeError on an empty expression it accepts
维护者通常 1 天内回复
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 80/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- typescript
- 领域
- backend
调研方向
从 packages/core-internal/src/shared/uriTemplate.ts 开始:parse() 会为空表达式 push 一个 name 为 undefined 的 part(~68-75),partToRegExp() 仍会为它生成 capture(~251),而 match() 会对它调用 name.replace(~283)。阅读 test/shared/uriTemplate.test.ts:229-230 中固定的容差断言,然后结合现有测试套件运行 issue 中的复现文件。完成意味着对于 '{}' 和 '/api/{}',match() 返回 null(或构造函数以清晰的 Error 拒绝),并且所有测试通过。
由索引模型根据 Issue 内容生成。
描述
Summary
UriTemplate accepts an expression with no variable name - {}, {,}, { }, {*} - but match() fails on it with a raw TypeError, while expand() and variableNames handle the same template without error.
const template = new UriTemplate('{}');
template.variableNames; // []
template.expand({}); // ''
template.match('abc'); // TypeError: Cannot read properties of undefined (reading 'replace')
The same happens when the empty expression sits next to literal text: /api/{} throws for /api/x.
UriTemplate is part of the public surface (@modelcontextprotocol/server re-exports it), and McpServer calls match() while resolving resources/read against a registered resource template (packages/server/src/server/mcp.ts:636). With new ResourceTemplate('test://{}', { list: undefined }) registered, a resources/read for test://abc answers:
{"jsonrpc":"2.0","id":2,"error":{"code":-32603,"message":"Cannot read properties of undefined (reading 'replace')"}}
A read that should not match the template - or should be refused when the template is registered - fails with an internal error that carries the implementation TypeError to the client.
Why this is a bug
RFC 6570 section 2.2 requires an expression to contain at least one variable specification, so {} is not a valid template. The class already chose to tolerate it: new UriTemplate('{}') and new UriTemplate('{,}') are pinned as not throwing in packages/core-internal/test/shared/uriTemplate.test.ts:229-230, and both variableNames and expand() handle that input without error. match() is the one surface that fails, and it fails with an implementation exception instead of a defined result.
Either the constructor should reject the template with a clear error, or match() should treat the expression as a non-match. Today it does neither.
Root cause
parse()computesnames = []for an empty expression and pushes a part withname: undefined(packages/core-internal/src/shared/uriTemplate.ts:68,75).partToRegExp()still emits one capture pattern for a non-query operator, carrying that undefined name (packages/core-internal/src/shared/uriTemplate.ts:251).match()loops over the captured names and callsname.replace('*', '')(packages/core-internal/src/shared/uriTemplate.ts:283), which throws.
Repro
Against current main (5ee54ee30ddc00a914a3d981880f87fdce0e6246), add this file as packages/core-internal/test/shared/uriTemplate-empty-expression.test.ts:
import { describe, expect, it } from 'vitest';
import { UriTemplate } from '../../src/shared/uriTemplate';
describe('UriTemplate empty expressions', () => {
it('constructor accepts an empty expression and expand() drops it', () => {
const template = new UriTemplate('{}');
expect(template.variableNames).toEqual([]);
expect(template.expand({})).toBe('');
});
it('match() should not throw for the template the constructor accepts', () => {
const template = new UriTemplate('{}');
expect(template.match('abc')).toBeNull();
});
it('a template with literal text still destroys match()', () => {
const template = new UriTemplate('/api/{}');
expect(template.match('/api/x')).toBeNull();
});
});
Run:
cd packages/core-internal
pnpm vitest run test/shared/uriTemplate.test.ts
pnpm vitest run test/shared/uriTemplate-empty-expression.test.ts
The existing suite stays green (38 passed); the new file reports 2 failed | 1 passed. The two failures are:
FAIL test/shared/uriTemplate-empty-expression.test.ts > UriTemplate empty expressions > match() should not throw for the template the constructor accepts
TypeError: Cannot read properties of undefined (reading 'replace')
❯ UriTemplate.match src/shared/uriTemplate.ts:283:36
281| const { name, exploded } = name_!;
282| const value = match[i + 1]!;
283| const cleanName = name.replace('*', '');
| ^
284|
285| result[cleanName] = exploded && value.includes(',') ? valu…
❯ test/shared/uriTemplate-empty-expression.test.ts:14:25
FAIL test/shared/uriTemplate-empty-expression.test.ts > UriTemplate empty expressions > a template with literal text still destroys match()
TypeError: Cannot read properties of undefined (reading 'replace')
❯ UriTemplate.match src/shared/uriTemplate.ts:283:36
❯ test/shared/uriTemplate-empty-expression.test.ts:19:25
The published package shows the same behavior. With @modelcontextprotocol/server 2.4.0 installed:
import { UriTemplate } from '@modelcontextprotocol/server';
const t = new UriTemplate('{}');
console.log(t.variableNames); // []
console.log(t.expand({})); // ""
console.log(t.match('abc')); // TypeError: Cannot read properties of undefined (reading 'replace')
Expected vs actual
Expected: match() returns null for a URI that has no matching named variable, as expand() returns nothing for the same expression - or the constructor rejects the malformed template with a clear Error.
Actual: match() throws TypeError: Cannot read properties of undefined (reading 'replace'), which escapes as an internal error on resources/read.
Environment
mainat5ee54ee30ddc00a914a3d981880f87fdce0e6246(2026-11-18), and the published@modelcontextprotocol/server2.4.0.- Node.js 24.10.0, pnpm 10.26.1, zod 4.3.6.
- No network access is needed; every repro step is in-process.
Possible fixes
- Reject the template at construction, which matches RFC 6570: after the names are parsed, throw a clear
Errorwhennames.length === 0. This changesnew UriTemplate('{}')andnew UriTemplate('{,}')from tolerated to rejected and needs the two assertions aturiTemplate.test.ts:229-230updated. - Keep the tolerance: do not push a part whose
namesare empty inparse(), or return no patterns frompartToRegExp()whennames.length === 0.expand()andvariableNameskeep their current results, andmatch()simply has no variable to capture instead of throwing. This is the smaller change and keeps the pinned tests.
I reproduced the unit failure and the resources/read response above. I used Claude Code to reproduce this defect and draft this report.
🤖 Generated with Claude Code
- 主要语言
- TypeScript
- 星标
- 13.8k
- 派生
- 2.5k
- 平均合并
- 6 天 23 小时
- 30 天内合并 PR
- 2
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
modelcontextprotocol/typescript-sdk 的其他 Issue
-
v2
难度 1/5 1 小时以内 新手友好度 72/100
modelcontextprotocol/typescript-sdk#3270 · 1 条评论 ·
维护者通常 1 天内回复
-
`callTool` with `allowInputRequired: true` throws on an `input_required` result when the tool declares an `outputSchema`可能已有人在做 @mgoldsborough 于 6 天前认领。 未关闭v2
难度 2/5 1-3 小时 新手友好度 78/100
modelcontextprotocol/typescript-sdk#3249 ·
维护者通常 1 天内回复
-
v2
难度 2/5 1-3 小时 新手友好度 68/100
modelcontextprotocol/typescript-sdk#3243 · 3 条评论 ·
维护者通常 1 天内回复
-
TypeScript client accepts an unknown `resultType` in the first `server/discover` reply可能已有人在做 @Tidehyc 于 7 天前认领。 未关闭v2
难度 2/5 1-3 小时 新手友好度 72/100
modelcontextprotocol/typescript-sdk#3235 · 3 条评论 ·
维护者通常 1 天内回复
-
`OpenIdProviderDiscoveryMetadataSchema` strips `revocation_endpoint`, so clients can't revoke against OIDC-only authorization servers可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭v1 v2
难度 2/5 1-3 小时 新手友好度 78/100
modelcontextprotocol/typescript-sdk#3213 · 3 条评论 ·
维护者通常 1 天内回复
查看 modelcontextprotocol/typescript-sdk 的全部 Issue
相似的 Issue
-
clawsweeper:intake:bug clawsweeper:intake:needs-environment
难度 2/5 1-3 小时 新手友好度 82/100
openclaw/openclaw#212827 · 1 条评论 · 1 个 reaction ·
维护者通常 1 天内回复
-
KT-2245 e2e specs that call the backend directly fall back to the developer's own Kronn on :3140未关闭
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
Use one constant for the 10,000-character input limit可能已有人在做 @NginxL 今天认领。 未关闭good first issue refactor
难度 2/5 1-3 小时 新手友好度 78/100
aniru-dh21/snap-validate#51 ·
维护者通常 1 天内回复
-
dependencies effort: small good first issue priority: low
难度 1/5 1-3 小时 新手友好度 88/100
TMHSDigital/screencast-mcp#509 ·
维护者通常 1 天内回复
-
from-review tech-debt testing
难度 2/5 1-3 小时 新手友好度 82/100
blamechris/chroxy#9520 ·
维护者通常 1 天内回复