semantic_code_edit_mcp/tools/set_context.rs
1use std::path::PathBuf;
2
3use crate::state::SemanticEditTools;
4use anyhow::Result;
5use mcplease::{
6 traits::{Tool, WithExamples},
7 types::Example,
8};
9use serde::{Deserialize, Serialize};
10
11/// Set the working context path for a session
12#[derive(Serialize, Deserialize, Debug, schemars::JsonSchema)]
13#[serde(rename = "set_context")]
14pub struct SetContext {
15 /// Directory path to set as context.
16 /// Subsequent to calling this, any relative paths will be relative to this directory
17 path: String,
18 // temporarily commented out
19 // /// Session identifier can be absolutely any string, as long as it's unlikely to collide with another session, (ie not "claude")
20 // /// You will need to provide this to subsequent tool calls, so short and memorable but unique is probably best. Be creative!
21 // ///
22 // /// This is currently necessary in order to isolate state between conversations because MCP does
23 // /// not currently provide any session identifier.
24 // /// Hopefully eventually this will be handled by the protocol.",
25 // session_id: String,
26}
27
28impl WithExamples for SetContext {
29 fn examples() -> Vec<Example<Self>> {
30 vec![Example {
31 description: "setting context to a development project",
32 item: Self {
33 path: "/usr/local/projects/cobol".into(),
34 // session_id: "GraceHopper1906".into(),
35 },
36 }]
37 }
38}
39
40impl Tool<SemanticEditTools> for SetContext {
41 fn execute(self, state: &mut SemanticEditTools) -> Result<String> {
42 let Self { path } = self;
43 let path = PathBuf::from(&*shellexpand::tilde(&path));
44 let response = format!(
45 "Set context to {path} for session.\n",
46 path = path.display()
47 );
48 state.set_context(None, path)?;
49 Ok(response)
50 }
51}