onerios-mcp-server
Version:
OneriosMCP server providing memory, backlog management, file operations, and utility functions for enhanced AI assistant capabilities
223 lines (222 loc) • 8.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.textProcessing = exports.TextProcessing = void 0;
const zod_1 = require("zod");
class TextProcessing {
constructor() {
this.tools = [
{
name: 'analyze_text',
description: 'Analyze text and provide statistics',
inputSchema: zod_1.z.object({
text: zod_1.z.string().describe('Text to analyze'),
include_words: zod_1.z.boolean().optional().default(false).describe('Include word frequency analysis'),
}),
},
{
name: 'format_text',
description: 'Format text with various transformations',
inputSchema: zod_1.z.object({
text: zod_1.z.string().describe('Text to format'),
operation: zod_1.z.enum(['uppercase', 'lowercase', 'title_case', 'camel_case', 'snake_case', 'kebab_case'])
.describe('Formatting operation to apply'),
}),
},
{
name: 'search_replace',
description: 'Search and replace text with support for regex',
inputSchema: zod_1.z.object({
text: zod_1.z.string().describe('Text to process'),
search: zod_1.z.string().describe('Text or regex pattern to search for'),
replace: zod_1.z.string().describe('Replacement text'),
use_regex: zod_1.z.boolean().optional().default(false).describe('Whether to use regex for search'),
global: zod_1.z.boolean().optional().default(true).describe('Replace all occurrences'),
}),
},
{
name: 'extract_patterns',
description: 'Extract patterns from text using regex',
inputSchema: zod_1.z.object({
text: zod_1.z.string().describe('Text to search in'),
pattern: zod_1.z.string().describe('Regex pattern to match'),
flags: zod_1.z.string().optional().default('g').describe('Regex flags'),
}),
},
{
name: 'split_text',
description: 'Split text by delimiter or pattern',
inputSchema: zod_1.z.object({
text: zod_1.z.string().describe('Text to split'),
delimiter: zod_1.z.string().describe('Delimiter or regex pattern to split by'),
use_regex: zod_1.z.boolean().optional().default(false).describe('Whether delimiter is a regex'),
limit: zod_1.z.number().optional().describe('Maximum number of splits'),
}),
},
];
}
getTools() {
return this.tools;
}
hasHandler(name) {
return this.tools.some(tool => tool.name === name);
}
async handleTool(name, args) {
switch (name) {
case 'analyze_text':
return this.analyzeText(args);
case 'format_text':
return this.formatText(args);
case 'search_replace':
return this.searchReplace(args);
case 'extract_patterns':
return this.extractPatterns(args);
case 'split_text':
return this.splitText(args);
default:
throw new Error(`Unknown tool: ${name}`);
}
}
async analyzeText(args) {
const text = args.text;
const lines = text.split('\n').length;
const words = text.split(/\s+/).filter(word => word.length > 0);
const characters = text.length;
const charactersNoSpaces = text.replace(/\s/g, '').length;
const sentences = text.split(/[.!?]+/).filter(s => s.trim().length > 0).length;
const paragraphs = text.split(/\n\s*\n/).filter(p => p.trim().length > 0).length;
let analysis = `Text Analysis:
- Lines: ${lines}
- Words: ${words.length}
- Characters: ${characters}
- Characters (no spaces): ${charactersNoSpaces}
- Sentences: ${sentences}
- Paragraphs: ${paragraphs}
- Average words per sentence: ${sentences > 0 ? (words.length / sentences).toFixed(2) : 0}`;
if (args.include_words) {
const wordFreq = words.reduce((freq, word) => {
const normalizedWord = word.toLowerCase().replace(/[^\w]/g, '');
if (normalizedWord) {
freq[normalizedWord] = (freq[normalizedWord] || 0) + 1;
}
return freq;
}, {});
const topWords = Object.entries(wordFreq)
.sort(([, a], [, b]) => b - a)
.slice(0, 10)
.map(([word, count]) => ` ${word}: ${count}`)
.join('\n');
analysis += `\n\nTop 10 Words:\n${topWords}`;
}
return {
content: [{
type: 'text',
text: analysis,
}],
};
}
async formatText(args) {
let result;
switch (args.operation) {
case 'uppercase':
result = args.text.toUpperCase();
break;
case 'lowercase':
result = args.text.toLowerCase();
break;
case 'title_case':
result = args.text.replace(/\w\S*/g, txt => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase());
break;
case 'camel_case':
result = args.text
.replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) => index === 0 ? word.toLowerCase() : word.toUpperCase())
.replace(/\s+/g, '');
break;
case 'snake_case':
result = args.text
.replace(/\W+/g, ' ')
.split(/ |\B(?=[A-Z])/)
.map(word => word.toLowerCase())
.join('_');
break;
case 'kebab_case':
result = args.text
.replace(/\W+/g, ' ')
.split(/ |\B(?=[A-Z])/)
.map(word => word.toLowerCase())
.join('-');
break;
default:
throw new Error(`Unknown formatting operation: ${args.operation}`);
}
return {
content: [{
type: 'text',
text: result,
}],
};
}
async searchReplace(args) {
let result;
if (args.use_regex) {
const flags = args.global ? 'g' : '';
const regex = new RegExp(args.search, flags);
result = args.text.replace(regex, args.replace);
}
else {
if (args.global) {
result = args.text.split(args.search).join(args.replace);
}
else {
result = args.text.replace(args.search, args.replace);
}
}
return {
content: [{
type: 'text',
text: result,
}],
};
}
async extractPatterns(args) {
try {
const regex = new RegExp(args.pattern, args.flags);
const matches = Array.from(args.text.matchAll(regex));
const results = matches.map((match, index) => {
const fullMatch = match[0];
const groups = match.slice(1);
return `Match ${index + 1}: "${fullMatch}"${groups.length > 0 ? ` (groups: ${groups.join(', ')})` : ''}`;
});
return {
content: [{
type: 'text',
text: results.length > 0 ? results.join('\n') : 'No matches found',
}],
};
}
catch (error) {
throw new Error(`Invalid regex pattern: ${args.pattern}`);
}
}
async splitText(args) {
let parts;
if (args.use_regex) {
const regex = new RegExp(args.delimiter);
parts = args.text.split(regex);
}
else {
parts = args.text.split(args.delimiter);
}
if (args.limit && args.limit > 0) {
parts = parts.slice(0, args.limit);
}
const result = parts.map((part, index) => `Part ${index + 1}: "${part}"`).join('\n');
return {
content: [{
type: 'text',
text: result,
}],
};
}
}
exports.TextProcessing = TextProcessing;
exports.textProcessing = new TextProcessing();