Skip to main content
This lesson walks through building a minimal MCP (Model Context Protocol) server in JavaScript that:
  • Stores prompts as JSON files (trackable in Git)
  • Registers prompts with an MCP server
  • Forwards prompt text to a local Ollama instance via its HTTP API
  • Provides a lightweight demo harness and shows how to test with the MCP Inspector
Contents:
  • Prompt file examples
  • Project setup and dependencies
  • Server implementation (server.js)
  • Using the MCP Inspector
  • Demo harness (simple-demo.js)
  • Sample output and best practices

Prompt files (examples)

Store each prompt as a separate JSON file under a prompts/ directory. This makes prompts easy to version and reuse. Summary of the example prompts: Example: greetings.json
Example: explain-concept.json
Example: code-review.json

Setup β€” node project and dependencies

Initialize the project and install the libraries used in this example:
Notes:
If you see a warning about β€œModule type of file … is not specified”, add "type": "module" to your package.json to explicitly enable ESM and avoid reparsing warnings.

Server implementation (server.js)

This server does the following:
  • Registers three prompts (greeting, explain-concept, code-review) with an MCP server
  • Sends prompt text to a local Ollama server using the Ollama HTTP generate endpoint
  • Starts a STDIO transport for MCP message exchange (common for MCP workflows)
Save the script below as server.js and run it with node server.js.
Key details:
  • The server uses sendToOllama() to POST to http://localhost:11434/api/generate.
  • Each registerPrompt call gives a prompt ID, metadata (title/description), and a handler that formats the final prompt text and returns a message bag.
  • The server connects over STDIO via StdioServerTransport so it can be inspected or proxied by MCP tools.

Using the MCP Inspector to test

The Model Context Protocol Inspector lets you inspect the prompts, tools, and resources your MCP server exposes. Install/run:
Example inspector startup (trimmed):
Once connected, the Inspector UI will list your greeting, explain-concept, and code-review prompts. You can invoke them interactively.

Simple demo harness (simple-demo.js)

This demo script spawns server.js, watches stdout until it sees Server ready, and then demonstrates how you might invoke prompts. The actual MCP invocation is left to the Inspector or an MCP client; this harness focuses on starting and verifying the server. Save as simple-demo.js:

Sample run output (trimmed)

Notes and best practices

  • Store prompt templates as files in a Git repo to gain version history, rollbacks, and collaboration.
  • Document each prompt’s template and variable schema (e.g., variables array) so clients know what to pass.
  • For production servers, add:
    • Robust error handling and retries for downstream calls (Ollama)
    • Structured logging and metrics
    • Input validation (e.g., use zod or another validator) on prompt parameters
    • Rate limiting and authentication where appropriate
  • If you hit Node module-type warnings, add "type": "module" to package.json to enable ESM.

Conclusion

This tutorial demonstrates registering reusable prompt definitions in an MCP server, forwarding prompt text to a local Ollama instance, and validating behavior with the MCP Inspector and a small demo harness. The approach enables shared, version-controlled prompt management across teams and services.

Watch Video