text-processing-tools.tsโข3.39 kB
/**
* Text Processing Tools
*
* Example MCP tools for text manipulation and analysis.
*/
import { z } from "zod";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import type { MCPModule } from "../types/index.js";
/**
* Tool registration function for text processing operations
*/
async function register(server: McpServer): Promise<void> {
// Register text transformation tool
server.registerTool(
"transform-text",
{
title: "Text Transformer",
description: "Transform text case and format",
inputSchema: {
text: z.string().describe("Text to transform"),
operation: z.enum([
"uppercase",
"lowercase",
"capitalize",
"reverse",
"word-count"
]).describe("Transformation operation to apply")
}
},
async ({ text, operation }) => {
let result: string;
switch (operation) {
case "uppercase":
result = text.toUpperCase();
break;
case "lowercase":
result = text.toLowerCase();
break;
case "capitalize":
result = text.split(" ")
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join(" ");
break;
case "reverse":
result = text.split("").reverse().join("");
break;
case "word-count":
const wordCount = text.trim().split(/\s+/).filter(word => word.length > 0).length;
result = `Word count: ${wordCount}`;
break;
default:
result = "Unknown operation";
}
return {
content: [
{
type: "text",
text: result
}
]
};
}
);
// Register text analysis tool
server.registerTool(
"analyze-text",
{
title: "Text Analyzer",
description: "Analyze text and provide statistics",
inputSchema: {
text: z.string().describe("Text to analyze")
}
},
async ({ text }) => {
const characterCount = text.length;
const characterCountNoSpaces = text.replace(/\s/g, "").length;
const wordCount = text.trim().split(/\s+/).filter(word => word.length > 0).length;
const sentenceCount = text.split(/[.!?]+/).filter(sentence => sentence.trim().length > 0).length;
const paragraphCount = text.split(/\n\s*\n/).filter(paragraph => paragraph.trim().length > 0).length;
const analysis = [
`๐ Text Analysis Results:`,
`Characters: ${characterCount}`,
`Characters (no spaces): ${characterCountNoSpaces}`,
`Words: ${wordCount}`,
`Sentences: ${sentenceCount}`,
`Paragraphs: ${paragraphCount}`,
`Average words per sentence: ${sentenceCount > 0 ? (wordCount / sentenceCount).toFixed(2) : "0"}`,
`Reading time (approx): ${Math.ceil(wordCount / 200)} minutes`
].join("\n");
return {
content: [
{
type: "text",
text: analysis
}
]
};
}
);
}
/**
* Export the MCP module
*/
export const textProcessingTools: MCPModule = {
register,
metadata: {
name: "text-processing-tools",
description: "Text manipulation and analysis tools for MCP",
version: "1.0.0",
author: "PCN"
}
};
export default textProcessingTools;