Skip to content

Commit a33c305

Browse files
feat(core): add deprecate() warn-once helper for v1-compat shims
1 parent 9ed62fe commit a33c305

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

.changeset/deprecate-helper.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@modelcontextprotocol/core': patch
3+
---
4+
5+
Add internal `deprecate()` warn-once helper for v1-compat shims.

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,4 @@ export * from './validators/fromJsonSchema.js';
4949

5050
// Core types only - implementations are exported via separate entry points
5151
export type { JsonSchemaType, JsonSchemaValidator, jsonSchemaValidator, JsonSchemaValidatorResult } from './validators/types.js';
52+
export { deprecate } from './util/deprecate.js';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const _warned = new Set<string>();
2+
3+
/**
4+
* Emits a one-time deprecation warning to stderr. Subsequent calls with the
5+
* same `key` are no-ops for the lifetime of the process.
6+
*
7+
* Used by v1-compat shims to nudge consumers toward the v2-native API without
8+
* spamming logs on hot paths (e.g. per-tool registration).
9+
*
10+
* @internal
11+
*/
12+
export function deprecate(key: string, msg: string): void {
13+
if (_warned.has(key)) return;
14+
_warned.add(key);
15+
// eslint-disable-next-line no-console
16+
console.warn(`[mcp-sdk] DEPRECATED: ${msg}`);
17+
}
18+
19+
/** @internal exposed for tests */
20+
export function _resetDeprecationWarnings(): void {
21+
_warned.clear();
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { describe, it, expect, vi, afterEach } from 'vitest';
2+
import { deprecate, _resetDeprecationWarnings } from '../../src/util/deprecate.js';
3+
4+
describe('deprecate', () => {
5+
afterEach(() => _resetDeprecationWarnings());
6+
it('warns once per key', () => {
7+
const spy = vi.spyOn(console, 'warn').mockImplementation(() => {});
8+
deprecate('k', 'msg');
9+
deprecate('k', 'msg');
10+
deprecate('other', 'msg2');
11+
expect(spy).toHaveBeenCalledTimes(2);
12+
spy.mockRestore();
13+
});
14+
});

0 commit comments

Comments
 (0)