Powrót do: Playwright z AI
Usprawniamy serwer MCP – własne prompty i nowe narzędzia
Prompts (szablony zapytań)
Prompts to szablonowe wiadomości i scenariusze konwersacji, które można wielokrotnie wykorzystywać, by ułatwić interakcję LLM z serwerem. Innymi słowy, prompty definiują pewien wzorzec dialogu np. predefiniowane pytanie użytkownika lub komunikat systemowy, który może zostać użyty w różnych sytuacjach.
W protokole MCP pozwalają one tworzyć powtarzalne sekwencje wiadomości (np. stałe instrukcje dla modelu) oraz parametryzować je danymi wejściowymi.
Każdy z poniższych promptów definiuje wzorzec wiadomości z ewentualnymi parametrami. Dzięki temu interakcje z modelem mogą być ustandaryzowane i wielokrotnie wykorzystywane w różnych kontekstach (np. w wielu sesjach). Zarejestrowane prompty pojawiają się jako dostępne akcje dla klienta (np. Copilota) i użytkownika, co ułatwia tworzenie złożonych zapytań na podstawie gotowych szablonów.
Jaki REST API wykorzystamy?
Będziemy bazować na darmowym API: https://restcountries.com/
Udostępnia ono endpointy do pobrania informacji o danym kraju:
https://restcountries.com/v3.1/name/poland
Lub o wszystkich krajach (tu musimy podać interesujące nas pola):
https://restcountries.com/v3.1/all?fields=name,capital,region,population,area,flags,languages,currencies
Cały wynikowy kod
Zawartość pliku main.ts:
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { z } from "zod"; const server = new McpServer({ name: "mcp-server-countries", version: "1.0.0", }); server.registerPrompt( "get-all-countries-prompt", { title: "Get all countries details", description: "Get all countries details from the REST API", inputSchema: {}, }, () => ({ messages: [ { role: "user", content: { type: "text", text: `Use tool to get all countries details from the REST API`, }, }, ], }) ); server.registerPrompt( "get-a-country-prompt", { title: "Get a specific country details", description: "Get a specific country details from the REST API", argsSchema: { countryName: z.string() }, }, ({ countryName }) => ({ messages: [ { role: "user", content: { type: "text", text: `Use tool to get a specific country details "${countryName}" from the REST API`, }, }, ], }) ); server.registerTool( "get-all-countries", { title: "Get all countries", description: "Tool to get all countries from the REST API", inputSchema: {}, }, async () => { const response = await fetch( "https://restcountries.com/v3.1/all?fields=name,capital,region,population,area,flags,languages,currencies" ); const data = await response.json(); return { content: [ { type: "text", text: JSON.stringify(data), }, ], }; } ); server.registerTool( "get-a-country", { title: "Get a specific country details", description: "Tool to get a specific country details from the REST API based on country name", inputSchema: { countryName: z.string().describe("The name of the country to get"), }, }, async ({ countryName }) => { const response = await fetch( `https://restcountries.com/v3.1/name/${countryName}` ); const text = await response.text(); return { content: [ { type: "text", text, }, ], }; } ); const transport = new StdioServerTransport(); server.connect(transport);
Prompt do testów serwera MCP
Przykładowe prompty do testów:
Use tool to get information how many countries use Spanish as official language
Use tool to get information about country Sweden and country Norway and tell me what country has bigger area