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.
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.
/*---------------------------------------------------------------------------------------------
mcpSamplingLog.ts ×7
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Disposable } from '../../../../base/common/lifecycle.js';
import { localize } from '../../../../nls.js';
import { ObservableMemento, observableMemento } from '../../../../platform/observable/common/observableMemento.js';
import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';
import { IMcpServer } from './mcpTypes.js';
import { MCP } from './modelContextProtocol.js';
const enum Constants {
SamplingRetentionDays = 7,
MsPerDay = 24 * 60 * 60 * 1000,
SamplingRetentionMs = SamplingRetentionDays * MsPerDay,
SamplingLastNMessage = 30,
}
export interface ISamplingStoredData {
// UTC day ordinal of the first bin in the bins
head: number;
// Requests per day, max length of `Constants.SamplingRetentionDays`
bins: number[];
// Last sampling requests/responses
lastReqs: { request: MCP.SamplingMessage[]; response: string; at: number; model: string }[];
}
const samplingMemento = observableMemento<ReadonlyMap<string, ISamplingStoredData>>({
defaultValue: new Map(),
key: 'mcp.sampling.logs',
toStorage: v => JSON.stringify(Array.from(v.entries())),
fromStorage: v => new Map(JSON.parse(v)),
});
export class McpSamplingLog extends Disposable {
private readonly _logs: { [K in StorageScope]?: ObservableMemento<ReadonlyMap<string, ISamplingStoredData>> } = {};
constructor(
@IStorageService private readonly _storageService: IStorageService,
) {
super();
}
public has(server: IMcpServer): boolean {
const storage = this._getLogStorageForServer(server);
return storage.get().has(server.definition.id);
}
public get(server: IMcpServer): Readonly<ISamplingStoredData | undefined> {
const storage = this._getLogStorageForServer(server);
return storage.get().get(server.definition.id);
}
public getAsText(server: IMcpServer): string {
const storage = this._getLogStorageForServer(server);
const record = storage.get().get(server.definition.id);
if (!record) {
return '';
}
const parts: string[] = [];
const total = record.bins.reduce((sum, value) => sum + value, 0);
parts.push(localize('mcp.sampling.rpd', '{0} total requests in the last 7 days.', total));
parts.push(this._formatRecentRequests(record));
return parts.join('\n');
}
private _formatRecentRequests(data: ISamplingStoredData): string {
if (!data.lastReqs.length) {
return '\nNo recent requests.';
}
const result: string[] = [];
for (let i = 0; i < data.lastReqs.length; i++) {
const { request, response, at, model } = data.lastReqs[i];
result.push(`\n[${i + 1}] ${new Date(at).toISOString()} ${model}`);
result.push(' Request:');
for (const msg of request) {
const role = msg.role.padEnd(9);
let content = '';
if ('text' in msg.content && msg.content.type === 'text') {
content = msg.content.text;
} else if ('data' in msg.content) {
content = `[${msg.content.type} data: ${msg.content.mimeType}]`;
}
result.push(` ${role}: ${content}`);
}
result.push(' Response:');
result.push(` ${response}`);
}
return result.join('\n');
}
public async add(server: IMcpServer, request: MCP.SamplingMessage[], response: string, model: string) {
const now = Date.now();
const utcOrdinal = Math.floor(now / Constants.MsPerDay);
const storage = this._getLogStorageForServer(server);
const next = new Map(storage.get());
let record = next.get(server.definition.id);
if (!record) {
record = {
head: utcOrdinal,
bins: Array.from({ length: Constants.SamplingRetentionDays }, () => 0),
lastReqs: [],
};
} else {
for (let i = 0; i < (utcOrdinal - record.head) && i < Constants.SamplingRetentionDays; i++) {
record.bins.unshift(0);
}
}
// Increment the current day's bin (head)
record.bins[0]++;
record.lastReqs.unshift({ request, response, at: now, model });
while (record.lastReqs.length > Constants.SamplingLastNMessage) {
}
next.set(server.definition.id, record);
storage.set(next, undefined);
}
private _getLogStorageForServer(server: IMcpServer) {
const scope = server.readDefinitions().get().collection?.scope ?? StorageScope.WORKSPACE;
return this._logs[scope] ??= this._register(samplingMemento(scope, StorageTarget.MACHINE, this._storageService));
}
}