|
| 1 | +/** |
| 2 | + * MCPC Example: Unified Prompt Management |
| 3 | + * |
| 4 | + * This example demonstrates the new centralized prompt management system |
| 5 | + * that consolidates all prompts and templates into a unified structure |
| 6 | + * with dynamic content replacement capabilities. |
| 7 | + */ |
| 8 | + |
| 9 | +import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; |
| 10 | +import { mcpc } from "../../mod.ts"; |
| 11 | +import { jsonSchema } from "ai"; |
| 12 | +import { |
| 13 | + CompiledPrompts, |
| 14 | + PromptUtils, |
| 15 | + type ToolDefinition |
| 16 | +} from "../../src/prompts/index.ts"; |
| 17 | + |
| 18 | +// Type definitions for tool arguments |
| 19 | +interface AuditLogArgs { |
| 20 | + action: string; |
| 21 | + user?: string; |
| 22 | + resource: string; |
| 23 | + level: "info" | "warn" | "error"; |
| 24 | +} |
| 25 | + |
| 26 | +interface SecurityValidationArgs { |
| 27 | + operation: string; |
| 28 | + path: string; |
| 29 | +} |
| 30 | + |
| 31 | +// Define available tools with enhanced descriptions |
| 32 | +const availableTools: ToolDefinition[] = [ |
| 33 | + { |
| 34 | + name: "@wonderwhy-er/desktop-commander.list_directory", |
| 35 | + description: "List directory contents with enhanced metadata, filtering capabilities, and security analysis" |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "@wonderwhy-er/desktop-commander.create_directory", |
| 39 | + description: "Create new directories with automatic permission setup, structure validation, and audit logging" |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "@wonderwhy-er/desktop-commander.move_file", |
| 43 | + description: "Move files with intelligent conflict resolution, backup creation, and integrity verification" |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "@wonderwhy-er/desktop-commander.delete_file", |
| 47 | + hide: true |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "@wonderwhy-er/desktop-commander.read_file", |
| 51 | + hide: true |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "code-runner.__ALL__" |
| 55 | + } |
| 56 | +]; |
| 57 | + |
| 58 | +// Generate dynamic descriptions using the prompt management system |
| 59 | +const publicTools = PromptUtils.generateToolList(availableTools); |
| 60 | +const hiddenTools = PromptUtils.generateHiddenToolList(availableTools); |
| 61 | +const wildcardTools = availableTools |
| 62 | + .filter(tool => tool.name.includes("__ALL__")) |
| 63 | + .map(tool => `<tool name="${tool.name}"/>`) |
| 64 | + .join('\n'); |
| 65 | + |
| 66 | +// Use the centralized template system with local description |
| 67 | +const fileOperationsDescription = `Advanced file management system with sophisticated tool management and security features. |
| 68 | +
|
| 69 | +**Public Tools with Enhanced Descriptions:** |
| 70 | +{publicTools} |
| 71 | +
|
| 72 | +**Internal Tools (Not Exposed to Users):** |
| 73 | +{hiddenTools} |
| 74 | +
|
| 75 | +**Wildcard Tool Selection:** |
| 76 | +{wildcardTools} |
| 77 | +
|
| 78 | +**Security Features:** |
| 79 | +- All destructive operations require internal validation |
| 80 | +- Comprehensive audit logging for compliance |
| 81 | +- Automatic backup creation before modifications |
| 82 | +- Path validation and security checks |
| 83 | +- User permission verification |
| 84 | +
|
| 85 | +**Advanced Capabilities:** |
| 86 | +- Smart conflict resolution during file operations |
| 87 | +- Automatic organization suggestions |
| 88 | +- Duplicate detection and management |
| 89 | +- File integrity verification |
| 90 | +- Batch operation support with progress tracking`; |
| 91 | + |
| 92 | +const agentDescription = fileOperationsDescription |
| 93 | + .replace('{publicTools}', publicTools) |
| 94 | + .replace('{hiddenTools}', hiddenTools) |
| 95 | + .replace('{wildcardTools}', wildcardTools); |
| 96 | + |
| 97 | +export const server = await mcpc( |
| 98 | + [ |
| 99 | + { |
| 100 | + name: "unified-prompt-manager", |
| 101 | + version: "1.0.0", |
| 102 | + }, |
| 103 | + { capabilities: { tools: { listChanged: true } } }, |
| 104 | + ], |
| 105 | + [ |
| 106 | + { |
| 107 | + name: "prompt-managed-agent", |
| 108 | + |
| 109 | + // Use centralized description template |
| 110 | + description: agentDescription, |
| 111 | + |
| 112 | + deps: { |
| 113 | + mcpServers: { |
| 114 | + "@wonderwhy-er/desktop-commander": { |
| 115 | + command: "npx", |
| 116 | + args: ["-y", "@wonderwhy-er/desktop-commander@latest"], |
| 117 | + }, |
| 118 | + "code-runner": { |
| 119 | + command: "deno", |
| 120 | + args: ["run", "--allow-all", "jsr:@mcpc/code-runner-mcp/bin"], |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + ], |
| 126 | + // Demonstrate internal tool registration with centralized prompts |
| 127 | + (server) => { |
| 128 | + server.tool( |
| 129 | + "audit-logger", |
| 130 | + "Internal comprehensive audit logging with standardized format", |
| 131 | + jsonSchema<AuditLogArgs>({ |
| 132 | + type: "object", |
| 133 | + properties: { |
| 134 | + action: { |
| 135 | + type: "string", |
| 136 | + description: "The action being performed" |
| 137 | + }, |
| 138 | + user: { |
| 139 | + type: "string", |
| 140 | + description: "User performing the action" |
| 141 | + }, |
| 142 | + resource: { |
| 143 | + type: "string", |
| 144 | + description: "Resource being accessed" |
| 145 | + }, |
| 146 | + level: { |
| 147 | + type: "string", |
| 148 | + enum: ["info", "warn", "error"], |
| 149 | + description: "Log level" |
| 150 | + } |
| 151 | + }, |
| 152 | + required: ["action", "resource", "level"] |
| 153 | + }), |
| 154 | + (args) => { |
| 155 | + // Use centralized audit log template |
| 156 | + const logMessage = CompiledPrompts.auditLog({ |
| 157 | + timestamp: PromptUtils.formatTimestamp(), |
| 158 | + level: args.level.toUpperCase(), |
| 159 | + action: args.action, |
| 160 | + resource: args.resource, |
| 161 | + userInfo: PromptUtils.formatUserInfo(args.user) |
| 162 | + }); |
| 163 | + |
| 164 | + console.log("AUDIT LOG:", logMessage); |
| 165 | + |
| 166 | + return { |
| 167 | + content: [ |
| 168 | + { |
| 169 | + type: "text", |
| 170 | + text: `Audit log entry created: ${logMessage}` |
| 171 | + } |
| 172 | + ] |
| 173 | + }; |
| 174 | + }, |
| 175 | + true // internal tool |
| 176 | + ); |
| 177 | + |
| 178 | + server.tool( |
| 179 | + "security-validator", |
| 180 | + "Internal security validation using centralized templates", |
| 181 | + jsonSchema<SecurityValidationArgs>({ |
| 182 | + type: "object", |
| 183 | + properties: { |
| 184 | + operation: { |
| 185 | + type: "string", |
| 186 | + description: "Operation to validate" |
| 187 | + }, |
| 188 | + path: { |
| 189 | + type: "string", |
| 190 | + description: "File or directory path" |
| 191 | + }, |
| 192 | + }, |
| 193 | + required: ["operation", "path"] |
| 194 | + }), |
| 195 | + (args) => { |
| 196 | + // Mock validation logic |
| 197 | + const isValid = !args.path.includes("/system") && |
| 198 | + !args.path.includes("/etc"); |
| 199 | + |
| 200 | + const message = isValid |
| 201 | + ? CompiledPrompts.securityPassed({ |
| 202 | + operation: args.operation, |
| 203 | + path: args.path |
| 204 | + }) |
| 205 | + : CompiledPrompts.securityFailed({ |
| 206 | + operation: args.operation, |
| 207 | + path: args.path |
| 208 | + }); |
| 209 | + |
| 210 | + return { |
| 211 | + content: [ |
| 212 | + { |
| 213 | + type: "text", |
| 214 | + text: message |
| 215 | + } |
| 216 | + ] |
| 217 | + }; |
| 218 | + }, |
| 219 | + true // internal tool |
| 220 | + ); |
| 221 | + |
| 222 | + console.log("✅ Unified prompt management system initialized!"); |
| 223 | + console.log("📝 All prompts are now centrally managed and dynamically generated"); |
| 224 | + } |
| 225 | +); |
| 226 | + |
| 227 | +const transport = new StdioServerTransport(); |
| 228 | +await server.connect(transport); |
| 229 | + |
| 230 | +/** |
| 231 | + * Benefits of the Unified Prompt Management System: |
| 232 | + * |
| 233 | + * 1. **Centralized Management:** |
| 234 | + * - All prompts stored in one location (/src/prompts/) |
| 235 | + * - Easy to update and maintain |
| 236 | + * - Consistent formatting across the application |
| 237 | + * |
| 238 | + * 2. **Dynamic Content Replacement:** |
| 239 | + * - Template variables for dynamic content |
| 240 | + * - Type-safe prompt compilation |
| 241 | + * - Runtime content substitution |
| 242 | + * |
| 243 | + * 3. **Reusable Templates:** |
| 244 | + * - Common patterns abstracted into templates |
| 245 | + * - Standardized message formats |
| 246 | + * - Consistent user experience |
| 247 | + * |
| 248 | + * 4. **Better Organization:** |
| 249 | + * - Prompts grouped by functionality |
| 250 | + * - Clear separation of concerns |
| 251 | + * - Easier to find and modify specific prompts |
| 252 | + * |
| 253 | + * 5. **Enhanced Maintainability:** |
| 254 | + * - Single source of truth for all text |
| 255 | + * - Reduces duplication |
| 256 | + * - Easier to test and validate |
| 257 | + * |
| 258 | + * Usage Examples: |
| 259 | + * |
| 260 | + * ```typescript |
| 261 | + * // Simple template usage |
| 262 | + * const message = CompiledPrompts.toolSuccess({ |
| 263 | + * toolName: "my-tool", |
| 264 | + * nextAction: "next-step", |
| 265 | + * currentAction: "current-step" |
| 266 | + * }); |
| 267 | + * |
| 268 | + * // Dynamic tool list generation |
| 269 | + * const toolList = PromptUtils.generateToolList(tools); |
| 270 | + * |
| 271 | + * // Template with content replacement |
| 272 | + * const description = ToolDescriptions.BASE_TEMPLATE |
| 273 | + * .replace('{description}', 'Your tool description') |
| 274 | + * .replace('{availableTools}', toolsList); |
| 275 | + * ``` |
| 276 | + * |
| 277 | + * This approach makes prompt management much more scalable and maintainable |
| 278 | + * as the MCPC ecosystem grows. |
| 279 | + */ |
0 commit comments