Implementation of MCP for LLM Observability capture to PostHig
Some checks failed
CI - Semantic Release / Semantic Release (push) Failing after 7m48s

This commit is contained in:
2025-07-13 20:42:19 -05:00
commit 05af3880f6
45 changed files with 16894 additions and 0 deletions

58
src/cli/index.ts Normal file
View File

@@ -0,0 +1,58 @@
import { Command } from 'commander';
import { Logger } from '../utils/logger.util.js';
import { VERSION, CLI_NAME } from '../utils/constants.util.js';
import posthogLlmCli from './posthog-llm.cli.js';
/**
* CLI entry point for the LLM Log MCP Server
*
* This file registers all CLI commands and handles command line parsing
*/
// Package description
const DESCRIPTION =
'A LLM Log Model Context Protocol (MCP) server implementation using TypeScript';
/**
* Run the CLI with the provided arguments
*
* @param args Command line arguments to process
* @returns Promise that resolves when CLI command execution completes
*/
export async function runCli(args: string[]) {
const cliLogger = Logger.forContext('cli/index.ts', 'runCli');
cliLogger.debug('Initializing CLI with arguments', args);
const program = new Command();
program.name(CLI_NAME).description(DESCRIPTION).version(VERSION);
// Register CLI commands
cliLogger.debug('Registering CLI commands...');
posthogLlmCli.register(program);
cliLogger.debug('CLI commands registered successfully');
// Handle unknown commands
program.on('command:*', (operands) => {
cliLogger.error(`Unknown command: ${operands[0]}`);
console.log('');
program.help();
process.exit(1);
});
// Parse arguments; default to help if no command provided
cliLogger.debug('Parsing CLI arguments');
// Special handling for top-level commands
if (args.length === 1) {
// Check if it's a known top-level command
const command = program.commands.find((cmd) => cmd.name() === args[0]);
if (command) {
command.outputHelp();
process.exit(0);
}
}
await program.parseAsync(args.length ? args : ['--help'], { from: 'user' });
cliLogger.debug('CLI command execution completed');
}

View File

@@ -0,0 +1,60 @@
import { Command } from 'commander';
import { Logger } from '../utils/logger.util.js';
import { handleCliError } from '../utils/error.util.js';
import postHogLlmController from '../controllers/posthog-llm.controller.js';
const logger = Logger.forContext('cli/posthog-llm.cli.ts');
/**
* Register PostHog LLM CLI commands
* @param program The Commander program instance
*/
function register(program: Command) {
const methodLogger = logger.forMethod('register');
methodLogger.debug('Registering PostHog LLM CLI commands...');
program
.command('llm-observability-mcp')
.description('Interact with the PostHog LLM API.')
.argument('<eventName>', 'The name of the event to capture.')
.argument('<distinctId>', 'The distinct ID of the user.')
.option(
'-p, --properties <properties...>',
'JSON string of event properties. Can be a single quoted string or raw key-value pairs.',
)
.action(async (eventName, distinctId, options) => {
const actionLogger = logger.forMethod('action:capture');
try {
actionLogger.debug(`CLI posthog-llm capture called`, {
eventName,
distinctId,
options,
});
// Handle JSON properties from command object
let properties = {};
if (options.properties && options.properties.length > 0) {
const propertiesString = options.properties.join(' ');
try {
properties = JSON.parse(propertiesString);
} catch {
// Try to fix common issues with JSON formatting
const fixedJson = propertiesString
.replace(/(\w+):/g, '"$1":') // Add quotes around keys
.replace(/'/g, '"'); // Replace single quotes with double quotes
properties = JSON.parse(fixedJson);
}
}
const args = { eventName, distinctId, properties };
await postHogLlmController.capture(args);
} catch (error) {
handleCliError(error);
}
});
methodLogger.debug('PostHog LLM CLI commands registered successfully');
}
export default { register };