@kya-os/mcp-i
Version:
COMING SOON:Production-ready MCP Identity with automatic registration, key rotation, and optimized performance
293 lines • 10 kB
JavaScript
/**
* Platform detection and client info generation for MCP-I
* Provides accurate runtime information across all deployment environments
*/
import * as fs from 'fs';
import * as path from 'path';
/**
* Get the SDK version from package.json
* Handles both ESM and CJS environments
*/
function getSdkVersion() {
try {
// Try multiple strategies to find package.json
const strategies = [
// Strategy 1: Use require for CJS environments
() => {
// @ts-ignore - require might not be available in ESM
if (typeof require !== 'undefined') {
try {
return require('../package.json').version;
}
catch {
// If direct require fails, try resolving from cwd
const packagePath = path.join(process.cwd(), 'node_modules', '@kya-os', 'mcp-i', 'package.json');
if (fs.existsSync(packagePath)) {
return JSON.parse(fs.readFileSync(packagePath, 'utf-8')).version;
}
}
}
throw new Error('require not available');
},
// Strategy 2: Try to read from expected locations
() => {
const possiblePaths = [
path.join(__dirname, '..', 'package.json'),
path.join(__dirname, '..', '..', 'package.json'),
path.join(process.cwd(), 'packages', 'mcp-i', 'package.json')
];
for (const packagePath of possiblePaths) {
if (fs.existsSync(packagePath)) {
return JSON.parse(fs.readFileSync(packagePath, 'utf-8')).version;
}
}
throw new Error('package.json not found');
},
// Strategy 3: Hardcoded fallback with build-time replacement
() => '0.1.0-alpha.3.6' // This should be replaced at build time
];
for (const strategy of strategies) {
try {
return strategy();
}
catch {
// Try next strategy
}
}
}
catch {
// Fallback version
}
return '0.1.0'; // Ultimate fallback
}
/**
* Detect the current runtime platform
*/
export function detectPlatform() {
// Check for browser environment
try {
// @ts-ignore
if (typeof window !== 'undefined' && typeof window.document !== 'undefined') {
return {
platform: 'browser',
runtime: 'browser',
capabilities: {
fileSystem: false,
asyncRuntime: true,
persistentStorage: true, // IndexedDB/LocalStorage
longRunning: false
}
};
}
}
catch {
// window is not defined, not in browser
}
// Check for Deno
// @ts-ignore - Deno global might not exist
if (typeof Deno !== 'undefined') {
return {
platform: 'deno',
runtime: 'deno',
// @ts-ignore
version: Deno.version?.deno,
capabilities: {
fileSystem: true,
asyncRuntime: true,
persistentStorage: true,
longRunning: true
}
};
}
// Check for Bun
// @ts-ignore - Bun global might not exist
if (typeof Bun !== 'undefined') {
return {
platform: 'bun',
runtime: 'bun',
// @ts-ignore
version: Bun.version,
capabilities: {
fileSystem: true,
asyncRuntime: true,
persistentStorage: true,
longRunning: true
}
};
}
// Node.js and Node-like environments
if (typeof process !== 'undefined' && process.versions?.node) {
// Detect specific deployment environments
let environment;
// Vercel Edge Runtime
if (process.env.VERCEL_EDGE || process.env.EDGE_RUNTIME) {
return {
platform: 'edge-light',
runtime: 'vercel-edge',
version: process.versions.node,
environment: 'vercel',
capabilities: {
fileSystem: false,
asyncRuntime: true,
persistentStorage: false, // No persistent storage in edge
longRunning: false
}
};
}
// Cloudflare Workers
// @ts-ignore - caches might not exist on globalThis
if (process.env.CF_WORKERS || (typeof globalThis !== 'undefined' && globalThis.caches)) {
return {
platform: 'edge-light',
runtime: 'cloudflare-workers',
environment: 'cloudflare',
capabilities: {
fileSystem: false,
asyncRuntime: true,
persistentStorage: true, // KV storage
longRunning: false
}
};
}
// AWS Lambda
if (process.env.AWS_LAMBDA_FUNCTION_NAME || process.env.LAMBDA_TASK_ROOT) {
environment = 'lambda';
return {
platform: 'node',
runtime: 'node',
version: process.versions.node,
environment,
capabilities: {
fileSystem: true, // /tmp is available
asyncRuntime: true,
persistentStorage: false, // Ephemeral
longRunning: false
}
};
}
// Vercel Functions (not edge)
if (process.env.VERCEL && !process.env.VERCEL_EDGE) {
environment = 'vercel-functions';
}
// Netlify Functions
if (process.env.NETLIFY) {
environment = 'netlify-functions';
}
// Docker
if (process.env.DOCKER_CONTAINER) {
environment = 'docker';
}
else {
// Try to detect Docker via cgroup (wrapped in try-catch for environments without /proc)
try {
if (fs.existsSync('/proc/1/cgroup') && fs.readFileSync('/proc/1/cgroup', 'utf-8').includes('docker')) {
environment = 'docker';
}
}
catch {
// Not in Docker or can't access /proc
}
}
// Next.js
if (process.env.NEXT_RUNTIME) {
environment = `nextjs-${process.env.NEXT_RUNTIME}`;
}
// Default Node.js
return {
platform: 'node',
runtime: 'node',
version: process.versions.node,
environment,
capabilities: {
fileSystem: true,
asyncRuntime: true,
persistentStorage: true,
longRunning: environment !== 'lambda' && environment !== 'vercel-functions'
}
};
}
// Unknown environment
return {
platform: 'unknown',
runtime: 'unknown',
capabilities: {
fileSystem: false,
asyncRuntime: true,
persistentStorage: false,
longRunning: false
}
};
}
/**
* Generate client info for registry requests
*/
export function generateClientInfo(options) {
const platformInfo = detectPlatform();
const sdkVersion = getSdkVersion();
// Determine default processing mode based on platform capabilities
let defaultProcessingMode = 'sync';
// Use async for environments that can't handle long-running requests
if (!platformInfo.capabilities.longRunning || platformInfo.platform === 'edge-light') {
defaultProcessingMode = 'async';
}
const clientInfo = {
sdkVersion,
language: 'javascript', // Always JavaScript at runtime
platform: platformInfo.platform,
processingMode: options?.processingMode || defaultProcessingMode
};
// Add runtime details if available
if (platformInfo.runtime !== platformInfo.platform) {
clientInfo.runtime = {
name: platformInfo.runtime,
version: platformInfo.version,
environment: platformInfo.environment
};
}
// Add custom metadata if provided
if (options?.customMetadata) {
clientInfo.metadata = options.customMetadata;
}
return clientInfo;
}
/**
* Get recommended configuration based on platform
*/
export function getPlatformRecommendations(platformInfo) {
const info = platformInfo || detectPlatform();
const recommendations = {
storage: 'auto',
transport: 'auto',
processingMode: 'sync',
logLevel: 'info'
};
// Storage recommendations
if (!info.capabilities.fileSystem || !info.capabilities.persistentStorage) {
recommendations.storage = 'memory';
}
else {
recommendations.storage = 'file';
}
// Transport recommendations
if (info.platform === 'edge-light' || info.platform === 'browser') {
recommendations.transport = 'fetch'; // Native fetch only
}
// Processing mode recommendations
if (!info.capabilities.longRunning) {
recommendations.processingMode = 'async';
}
// Log level recommendations
if (info.environment === 'lambda' || info.platform === 'edge-light') {
recommendations.logLevel = 'error'; // Minimize logging in serverless
}
return recommendations;
}
// Export a singleton instance of platform info for caching
let cachedPlatformInfo = null;
export function getCachedPlatformInfo() {
if (!cachedPlatformInfo) {
cachedPlatformInfo = detectPlatform();
}
return cachedPlatformInfo;
}
//# sourceMappingURL=platform-info.js.map