Working with MCP tools

MCP offers a way for servers to implement and expose tools to client. In Glamorous Toolkit, we forward these tools to the LLM integrations offered through chats and providers.

In this example, we will use the server from Working with MCP servers and add its tools to a chat.

First, we need to instantiate the process.

process := (GtExternalProcessBuilder new: 'npx')
		args: {'@modelcontextprotocol/server-filesystem'.
				'~'};
		pipeStdin;
		pipeStdout;
		pipeStderr;
		spawn
  

Then, we can connect to it with a client.

mcpClient := GtMcpClient new
		transport: (GtMcpStdioTransport new process: process)
  

Once we have a connection, we can then list or call tools programmatically

mcpClient listTools.

mcpClient callTool: 'list_allowed_directories' withArguments: {} asDictionary
  

or integrate them into our chat environent:

chat := GtLlmChat new.

mcpClient llmFunctionTools do: [ :aTool | chat provider addTool: aTool ].

chat sendMessage: 'List the directories you are allowed to access.'