Working with MCP prompts

MCP servers can generate prompts for users. In this example, we’ll use the fetch example server from the MCP community to show how to integrate them into our chats.

First, we need to set up the server. This requires uvx.

process := (GtExternalProcessBuilder new: 'uvx')
		args: {'mcp-server-fetch'};
		pipeStdin;
		pipeStdout;
		pipeStderr;
		spawn
  

We can then build an MCP client for it.

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

This particular client only has one prompt, fetch, which enables us to create Markdown-based messages from websites and add them to our chat to enrich the context.

In this example, we use the documentation site of MCP prompts to generate messages and add them to the chat.

chat := GtLlmChat new.

messages := mcpClient
	messagesFromPrompt: 'fetch'
	withArguments: {'url' -> 'https://modelcontextprotocol.io/docs/concepts/prompts'} asDictionary.

messages do: [:aMessage | chat addMessage: aMessage ].

chat sendMessage: 'Can you explain MCP prompts to me concisely?'
  

We can also list all prompts using GtMcpClient>>#listPrompts listPrompts ^ (self sendMethod: 'prompts/list' withParams: {} asDictionary) at: 'result' at: 'prompts' or get individual prompts without converting them to messages using GtMcpClient>>#getPrompt:withArguments: getPrompt: aString withArguments: anObject ^ (self sendMethod: 'prompts/get' withParams: {'name' -> aString. 'arguments' -> anObject} asDictionary) at: 'result' .