format_code
Automatically format code in a file by specifying the file path and programming language, ensuring consistent style and improved readability for developers.
Instructions
Format code in a file
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| language | No | Language of the code (e.g., javascript, python, rust) | |
| path | Yes | Path to the file to format |
Input Schema (JSON Schema)
{
"properties": {
"language": {
"description": "Language of the code (e.g., javascript, python, rust)",
"type": "string"
},
"path": {
"description": "Path to the file to format",
"type": "string"
}
},
"required": [
"path"
],
"type": "object"
}
Implementation Reference
- src/index.ts:246-269 (registration)Registration of the 'format_code' tool including its input schema, description, and annotations.mcpServer.registerTool({ name: 'format_code', description: 'Format code in a file', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path to the file to format' }, language: { type: 'string', description: 'Language of the code (e.g., javascript, python, rust)' } }, required: ['path'] }, annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: false } });
- src/router/operation-router.ts:446-454 (handler)Handler dispatch for 'format_code' operation in executeWithEdit method, which calls EditInstanceManager with a specific 'format' edit command.case 'format_code': return this.editInstanceManager.executeEditCommand(sessionId, { type: 'edit', params: { action: 'format', language: operation.params.language } });
- Low-level handler for 'edit' commands in executeEditCommand, which serializes params (including format action) and sends to the Edit process instance.case 'edit': // This is a simplified approach; in a real implementation, we would need // to handle different types of edits (insert, delete, replace, etc.) result = await instance.executeCommand(`edit ${JSON.stringify(command.params)}`); return { success: true, message: result };
- 'format_code' classified as a complex operation in analyzeComplexity method for routing decisions.const complexOperations = [ 'interactive_edit_session', 'format_code', 'complex_find_replace', 'merge_conflicts_resolution', 'bulk_edit_operation', 'edit_with_context_awareness' ];
- src/http/http-server.ts:257-276 (helper)REST API endpoint wrapper that calls the 'format_code' tool via JSON-RPC.this.app.post('/api/format', async (req, res) => { try { const formatRequest = parseMessage({ jsonrpc: '2.0', method: 'tools/call', params: { name: 'format_code', arguments: { path: req.body.path, language: req.body.language } }, id: 'rest-' + Date.now() }); const response = await this.mcpServer.handleMessage(formatRequest); res.json(response); } catch (error: any) { res.status(500).json({ error: error.message }); } });