refactor: clean up redundant serde aliases in tools schema

This commit is contained in:
Riz Ashraf committed 2026-10-10 03:54:16 +01:00
1 parent 27416e4ee9
commit 2ae343d968
6 files changed
+171 -191

No files matched your search

+1
View File
@@ -16,6 +16,7 @@ rmcp = { version = "3.4.0", features = ["server"] }
mcp-stdio = { version = "1.0.0", path = "../mcp-stdio" }
bytes = "1.12.1"
dashmap = "6.2.1"
schemars = "0.8"
[build-dependencies]
chrono = "0.4.45"
+25 -118
View File
@@ -1,3 +1,5 @@
mod tools;
use tools::*;
mod headless;
use headless::*;
use serde::{Deserialize, Serialize};
@@ -22,6 +24,22 @@ pub struct JsonRpcResponse {
pub error: Option<Value>,
}
pub fn tool_def<T: schemars::JsonSchema>(name: &str, fallback_desc: &str) -> serde_json::Value {
let schema = schemars::schema_for!(T);
let schema_val = serde_json::to_value(&schema).unwrap_or_default();
let desc = schema_val
.get("description")
.and_then(|d| d.as_str())
.filter(|s| !s.trim().is_empty())
.unwrap_or(fallback_desc)
.to_string();
serde_json::json!({
"name": name,
"description": desc,
"inputSchema": schema_val
})
}
pub async fn send_response(response: JsonRpcResponse) {
let msg = serde_json::to_string(&response).unwrap_or_else(|_| "{}".to_string());
tracing::info!(
@@ -977,124 +995,13 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
jsonrpc: "2.0".to_string(),
id,
result: Some(json!({
"tools":
[
{
"name": "nvim_buffer",
"description": "Core Text Editing: read, replace, save, and manipulate Neovim buffers in memory.",
"inputSchema": {
"type": "object",
"properties": {
"action": {
"type": "string",
"enum": ["read", "replace", "save", "undo", "redo", "create_scratch"],
"description": "Action to perform on the buffer"
},
"file": { "type": "string", "description": "Target file path (for read, replace, save)" },
"start_line": { "type": "integer", "description": "1-indexed start line (for read)" },
"end_line": { "type": "integer", "description": "1-indexed end line (for read)" },
"content": { "type": "string", "description": "Initial text content (for create_scratch)" },
"name": { "type": "string", "description": "Buffer display name (for create_scratch)" },
"steps": { "type": "integer", "description": "Number of undo/redo steps to apply (default: 1)" },
"edits": {
"type": "array",
"description": "Array of edits to apply sequentially (for replace). Grouped by file, applied in descending order.",
"items": {
"type": "object",
"properties": {
"file": { "type": "string" },
"start_line": { "type": "integer" },
"end_line": { "type": "integer" },
"replacement_content": { "type": "string" },
"expected_content": { "type": "string" }
},
"required": ["file", "start_line", "end_line", "replacement_content"]
}
}
},
"required": ["action"]
}
},
{
"name": "nvim_workspace",
"description": "Window & Editor State: list buffers, windows, focus splits, and manage cwd.",
"inputSchema": {
"type": "object",
"properties": {
"action": {
"type": "string",
"enum": ["list_buffers", "list_windows", "focus", "split", "cwd"],
"description": "Workspace operation to perform"
},
"win_id": { "type": "integer", "description": "Window ID to focus" },
"file": { "type": "string", "description": "File to open in split" },
"direction": { "type": "string", "enum": ["vertical", "horizontal"], "description": "Split orientation (default: vertical)" },
"path": { "type": "string", "description": "Target directory (for cwd action)" }
},
"required": ["action"]
}
},
{
"name": "nvim_intelligence",
"description": "Code Semantics: LSP queries (hover, definition, references, code_action, rename, diagnostics) and AST outlining/queries.",
"inputSchema": {
"type": "object",
"properties": {
"action": {
"type": "string",
"enum": ["hover", "definition", "references", "outline", "query", "diagnostics", "rename", "code_action"],
"description": "Intelligence action to perform"
},
"file": { "type": "string", "description": "Target file path" },
"line": { "type": "integer", "description": "1-indexed line number (for LSP)" },
"col": { "type": "integer", "description": "0-indexed column number (for LSP)" },
"new_name": { "type": "string", "description": "New name (for rename action)" },
"query": { "type": "string", "description": "Tree-sitter query string (for AST query)" },
"preset": { "type": "string", "description": "Query preset (e.g., 'functions', 'classes' for AST query)" }
},
"required": ["action"]
}
},
{
"name": "nvim_ui",
"description": "Visual Feedback: communicating visually with the human user via highlights and ghost text.",
"inputSchema": {
"type": "object",
"properties": {
"action": {
"type": "string",
"enum": ["highlight", "ghost_text", "clear"],
"description": "UI action"
},
"buf_id": { "type": "integer", "description": "Buffer ID to apply to" },
"start_line": { "type": "integer", "description": "1-indexed start line (for highlight)" },
"end_line": { "type": "integer", "description": "1-indexed end line (for highlight)" },
"line": { "type": "integer", "description": "1-indexed line number (for ghost_text)" },
"text": { "type": "string", "description": "Virtual text to display (for ghost_text)" },
"highlight_group": { "type": "string", "description": "Highlight group (e.g. 'Comment', 'IncSearch')" },
"duration_ms": { "type": "integer", "description": "Auto-clear duration in ms (for highlight, default: 5000)" }
},
"required": ["action"]
}
},
{
"name": "nvim_exec",
"description": "The Escape Hatch: execute lua read-only queries, run vimscript commands, or send commands to the terminal.",
"inputSchema": {
"type": "object",
"properties": {
"action": {
"type": "string",
"enum": ["lua", "vimscript", "terminal"],
"description": "Execution action"
},
"code": { "type": "string", "description": "Lua code or Vimscript command to execute" },
"command": { "type": "string", "description": "Shell command to send (for terminal)" }
},
"required": ["action"]
}
}
]
"tools": [
tool_def::<NvimBufferTool>("nvim_buffer", "Core Text Editing: read, replace, save, and manipulate Neovim buffers in memory."),
tool_def::<NvimWorkspaceTool>("nvim_workspace", "Window & Editor State: list buffers, windows, focus splits, and manage cwd."),
tool_def::<NvimIntelligenceTool>("nvim_intelligence", "Code Semantics: LSP queries (hover, definition, references, code_action, rename, diagnostics) and AST outlining/queries."),
tool_def::<NvimUiTool>("nvim_ui", "Visual Feedback: communicating visually with the human user via highlights and ghost text."),
tool_def::<NvimExecTool>("nvim_exec", "The Escape Hatch: execute lua read-only queries, run vimscript commands, or send commands to the terminal."),
]
})),
error: None,
+101
View File
@@ -0,0 +1,101 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
#[serde(tag = "action", rename_all = "snake_case")]
pub enum NvimBufferTool {
Read {
file: String,
start_line: Option<i64>,
end_line: Option<i64>,
},
Replace {
file: Option<String>,
start_line: Option<i64>,
end_line: Option<i64>,
replacement_content: Option<String>,
expected_content: Option<String>,
edits: Option<Vec<BufferEdit>>,
},
Save {
file: Option<String>,
},
Undo {
steps: Option<i64>,
},
Redo {
steps: Option<i64>,
},
CreateScratch {
name: Option<String>,
content: Option<String>,
},
}
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub struct BufferEdit {
pub file: String,
pub start_line: i64,
pub end_line: i64,
pub replacement_content: String,
pub expected_content: Option<String>,
}
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
#[serde(tag = "action", rename_all = "snake_case")]
pub enum NvimWorkspaceTool {
ListBuffers,
ListWindows,
Focus {
win_id: i64,
},
Split {
direction: Option<String>,
file: Option<String>,
buf_id: Option<i64>,
},
Cwd {
path: Option<String>,
},
}
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
#[serde(tag = "action", rename_all = "snake_case")]
pub enum NvimIntelligenceTool {
Hover { file: String, line: i64, col: i64 },
Definition { file: String, line: i64, col: i64 },
References { file: String, line: i64, col: i64 },
CodeAction { file: String, line: i64, col: i64 },
Rename { file: String, line: i64, col: i64, new_name: String },
Diagnostics { file: String },
Outline { file: String },
Query { file: String, query: Option<String>, preset: Option<String> },
}
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
#[serde(tag = "action", rename_all = "snake_case")]
pub enum NvimUiTool {
Highlight {
buf_id: Option<i64>,
start_line: i64,
end_line: Option<i64>,
highlight_group: Option<String>,
duration_ms: Option<i64>,
},
GhostText {
line: i64,
text: String,
highlight_group: Option<String>,
},
Clear {
buf_id: Option<i64>,
},
}
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
#[serde(tag = "action", rename_all = "snake_case")]
pub enum NvimExecTool {
Lua { code: String },
Vimscript { code: String },
Terminal { command: String },
}