Skip to main content
Glama
widjis
by widjis

ssh_set_working_directory

Change the current working directory for an SSH connection to navigate remote file systems and execute commands from a specific location.

Instructions

Set the current working directory for a connection

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
connectionIdYesSSH connection ID
workingDirectoryYesWorking directory path to set as current

Implementation Reference

  • The handler function for 'ssh_set_working_directory' tool. Parses input, retrieves connection context, verifies the directory exists on the remote server using 'test -d', updates the connection's currentWorkingDirectory, and returns success message.
    private async handleSetWorkingDirectory(args: unknown) {
      const params = SetWorkingDirectorySchema.parse(args);
      
      const context = connectionContexts.get(params.connectionId);
      if (!context) {
        throw new McpError(
          ErrorCode.InvalidParams,
          `Connection ID '${params.connectionId}' not found`
        );
      }
    
      try {
        // Verify the directory exists
        const result = await context.ssh.execCommand(`test -d "${params.workingDirectory}" && echo "exists"`);
        if (result.code !== 0) {
          throw new McpError(
            ErrorCode.InvalidParams,
            `Directory '${params.workingDirectory}' does not exist or is not accessible`
          );
        }
    
        // Set the working directory
        context.currentWorkingDirectory = params.workingDirectory;
        
        return {
          content: [
            {
              type: 'text',
              text: `Working directory set to: ${params.workingDirectory}`,
            },
          ],
        };
      } catch (error) {
        if (error instanceof McpError) {
          throw error;
        }
        throw new McpError(
          ErrorCode.InternalError,
          `Failed to set working directory: ${error instanceof Error ? error.message : String(error)}`
        );
      }
    }
  • Zod schema defining the input parameters for the ssh_set_working_directory tool: connectionId (string) and workingDirectory (string).
    const SetWorkingDirectorySchema = z.object({
      connectionId: z.string().describe('SSH connection ID'),
      workingDirectory: z.string().describe('Working directory path to set as current')
    });
  • src/index.ts:421-431 (registration)
    Registration of the 'ssh_set_working_directory' tool in the ListTools response, including name, description, and input schema matching the Zod schema.
      name: 'ssh_set_working_directory',
      description: 'Set the current working directory for a connection',
      inputSchema: {
        type: 'object',
        properties: {
          connectionId: { type: 'string', description: 'SSH connection ID' },
          workingDirectory: { type: 'string', description: 'Working directory path to set as current' }
        },
        required: ['connectionId', 'workingDirectory']
      },
    },
  • src/index.ts:513-514 (registration)
    Dispatch case in the CallToolRequest handler that routes to the handleSetWorkingDirectory method.
    case 'ssh_set_working_directory':
      return await this.handleSetWorkingDirectory(args);
  • Interface for connection context that stores the currentWorkingDirectory used by the set working directory handler.
    interface ConnectionContext {
      ssh: NodeSSH;
      currentWorkingDirectory?: string;
      defaultWorkingDirectory?: string;
    }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/widjis/mcp-ssh'

If you have feedback or need assistance with the MCP directory API, please join our Discord server