Skip to main content
Glama
resource-manager.ts1.87 kB
import { McpError, ErrorCode } from '@modelcontextprotocol/sdk/types.js'; export interface ResourceLimits { timeout?: number; maxFileSize?: number; maxTotalSize?: number; } export class ResourceManager { private totalSize = 0; constructor(private limits: ResourceLimits = {}) {} /** * Wraps an async operation with a timeout */ async withTimeout<T>( operation: Promise<T>, operationName: string, customTimeout?: number ): Promise<T> { const timeout = customTimeout || this.limits.timeout; if (!timeout) { return operation; } return Promise.race([ operation, new Promise<never>((_, reject) => { setTimeout(() => { reject(new McpError( ErrorCode.InternalError, `Operation timed out after ${timeout}ms` )); }, timeout); }) ]); } /** * Checks if a file size exceeds the limit */ checkFileSize(size: number, filePath: string): void { if (!this.limits.maxFileSize) return; if (size > this.limits.maxFileSize) { throw new McpError( ErrorCode.InvalidRequest, `File size (${size} bytes) exceeds maximum allowed size (${this.limits.maxFileSize} bytes)` ); } } /** * Tracks total size and checks against limit */ trackSize(size: number): void { this.totalSize += size; if (this.limits.maxTotalSize && this.totalSize > this.limits.maxTotalSize) { throw new McpError( ErrorCode.InvalidRequest, `Total size (${this.totalSize} bytes) exceeds maximum allowed size (${this.limits.maxTotalSize} bytes)` ); } } /** * Resets the total size counter */ resetSize(): void { this.totalSize = 0; } /** * Gets the current total size */ getTotalSize(): number { return this.totalSize; } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/davstr1/peekabooMCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server