src/vs/workbench/contrib/mcp/common/mcpSamplingLog.ts

135 LOC · 90 covered · 45 uncovered · 11 ranges · 5 concepts · 4 introducers · 6 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the filemcpSamplingLog.test|title=MCP - Sampling Log handles different content types|occurrence=1 · 0 introduced LOCmcpSamplingLog.test|titl…mcpSamplingLog.ts ×1 · 2 introduced LOCmcpSamplingLog.ts ×1mcpSamplingLog.ts ×1 · 3 introduced LOCmcpSamplingLog.ts ×1mcpSamplingLog.ts ×2 · 4 introduced LOCmcpSamplingLog.ts ×2mcpSamplingLog.ts ×7 · 81 introduced LOCmcpSamplingLog.ts ×7mcpSamplingLog.test|title=MCP - Sampling Log handles different content types|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/contrib/mcp/test/common/mcpSamplingLog.test|title=MCP - Sampling Log handles different content types|occurrence=1mcpSamplingLog.test|titl…mcpSamplingLog.test|title=MCP - Sampling Log handles multiple servers|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/contrib/mcp/test/common/mcpSamplingLog.test|title=MCP - Sampling Log handles multiple servers|occurrence=1mcpSamplingLog.test|titl…mcpSamplingLog.test|title=MCP - Sampling Log limits the number of stored requests|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/contrib/mcp/test/common/mcpSamplingLog.test|title=MCP - Sampling Log limits the number of stored requests|occurrence=1mcpSamplingLog.test|titl…mcpSamplingLog.test|title=MCP - Sampling Log logs a single request|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/contrib/mcp/test/common/mcpSamplingLog.test|title=MCP - Sampling Log logs a single request|occurrence=1mcpSamplingLog.test|titl…mcpSamplingLog.test|title=MCP - Sampling Log logs multiple requests on the same day|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/contrib/mcp/test/common/mcpSamplingLog.test|title=MCP - Sampling Log logs multiple requests on the same day|occurrence=1mcpSamplingLog.test|titl…mcpSamplingLog.test|title=MCP - Sampling Log shifts bins when adding requests on different days|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/contrib/mcp/test/common/mcpSamplingLog.test|title=MCP - Sampling Log shifts bins when adding requests on different days|occurrence=1mcpSamplingLog.test|titl…Focused file · src/vs/workbench/contrib/mcp/common/mcpSamplingLog.ts · 135 LOCcommon/mcpSamplingLog.ts

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- mcpSamplingLog.ts ×7
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { Disposable } from '../../../../base/common/lifecycle.js';
7 > import { localize } from '../../../../nls.js';
8 > import { ObservableMemento, observableMemento } from '../../../../platform/observable/common/observableMemento.js';
9 > import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';
10 > import { IMcpServer } from './mcpTypes.js';
11 > import { MCP } from './modelContextProtocol.js';
12 >
13 > const enum Constants {
14 > SamplingRetentionDays = 7,
15 > MsPerDay = 24 * 60 * 60 * 1000,
16 > SamplingRetentionMs = SamplingRetentionDays * MsPerDay,
17 > SamplingLastNMessage = 30,
18 > }
19 >
20 > export interface ISamplingStoredData {
21 > // UTC day ordinal of the first bin in the bins
22 > head: number;
23 > // Requests per day, max length of `Constants.SamplingRetentionDays`
24 > bins: number[];
25 > // Last sampling requests/responses
26 > lastReqs: { request: MCP.SamplingMessage[]; response: string; at: number; model: string }[];
27 > }
28 >
29 > const samplingMemento = observableMemento<ReadonlyMap<string, ISamplingStoredData>>({
30 > defaultValue: new Map(),
31 > key: 'mcp.sampling.logs',
32 > toStorage: v => JSON.stringify(Array.from(v.entries())),
33 > fromStorage: v => new Map(JSON.parse(v)),
34 > });
35 >
36 > export class McpSamplingLog extends Disposable {
37 > private readonly _logs: { [K in StorageScope]?: ObservableMemento<ReadonlyMap<string, ISamplingStoredData>> } = {};
38 >
39 > constructor(
40 > @IStorageService private readonly _storageService: IStorageService,
41 > ) {
42 > super();
43 > }
44 >
45 > public has(server: IMcpServer): boolean {
46 const storage = this._getLogStorageForServer(server);
47 return storage.get().has(server.definition.id);
48 }
50 > public get(server: IMcpServer): Readonly<ISamplingStoredData | undefined> {
51 const storage = this._getLogStorageForServer(server);
52 return storage.get().get(server.definition.id);
53 }
55 > public getAsText(server: IMcpServer): string {
56 const storage = this._getLogStorageForServer(server);
57 const record = storage.get().get(server.definition.id);
58 if (!record) {
59 return '';
60 }
61
62 const parts: string[] = [];
63 const total = record.bins.reduce((sum, value) => sum + value, 0);
64 parts.push(localize('mcp.sampling.rpd', '{0} total requests in the last 7 days.', total));
65
66 parts.push(this._formatRecentRequests(record));
67 return parts.join('\n');
68 }
70 > private _formatRecentRequests(data: ISamplingStoredData): string {
71 if (!data.lastReqs.length) {
72 return '\nNo recent requests.';
73 }
74
75 const result: string[] = [];
76 for (let i = 0; i < data.lastReqs.length; i++) {
77 const { request, response, at, model } = data.lastReqs[i];
78 result.push(`\n[${i + 1}] ${new Date(at).toISOString()} ${model}`);
79
80 result.push(' Request:');
81 for (const msg of request) {
82 const role = msg.role.padEnd(9);
83 let content = '';
84 if ('text' in msg.content && msg.content.type === 'text') {
85 content = msg.content.text;
86 } else if ('data' in msg.content) {
87 content = `[${msg.content.type} data: ${msg.content.mimeType}]`;
88 }
89 result.push(` ${role}: ${content}`);
90 }
91 result.push(' Response:');
92 result.push(` ${response}`);
93 }
94
95 return result.join('\n');
96 }
98 > public async add(server: IMcpServer, request: MCP.SamplingMessage[], response: string, model: string) {
99 > const now = Date.now();
100 > const utcOrdinal = Math.floor(now / Constants.MsPerDay);
101 > const storage = this._getLogStorageForServer(server);
102 >
103 > const next = new Map(storage.get());
104 > let record = next.get(server.definition.id);
105 > if (!record) {
106 > record = {
107 > head: utcOrdinal,
108 > bins: Array.from({ length: Constants.SamplingRetentionDays }, () => 0),
109 > lastReqs: [],
110 > };
111 > } else {
112 > // Shift bins back by daysSinceHead, dropping old days mcpSamplingLog.ts ×2
113 > for (let i = 0; i < (utcOrdinal - record.head) && i < Constants.SamplingRetentionDays; i++) {
114 > record.bins.pop(); mcpSamplingLog.ts ×1
115 > record.bins.unshift(0);
116 > }
117 > record.head = utcOrdinal; mcpSamplingLog.ts ×2
118 > }
120 > // Increment the current day's bin (head)
121 > record.bins[0]++;
122 > record.lastReqs.unshift({ request, response, at: now, model });
123 > while (record.lastReqs.length > Constants.SamplingLastNMessage) {
124 > record.lastReqs.pop(); mcpSamplingLog.ts ×1
125 > }
127 > next.set(server.definition.id, record);
128 > storage.set(next, undefined);
129 > }
130 >
131 > private _getLogStorageForServer(server: IMcpServer) {
132 > const scope = server.readDefinitions().get().collection?.scope ?? StorageScope.WORKSPACE;
133 > return this._logs[scope] ??= this._register(samplingMemento(scope, StorageTarget.MACHINE, this._storageService));
134 > }
135 > }