examples/

directory
v1.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 21, 2025 License: MIT

README

GoMCP Examples

This directory contains examples for using the GoMCP library.

Server Example

The server example demonstrates how to create and run an MCP server with stdio transport.

Features Demonstrated
  • Creating a server with server.NewServer()
  • Configuring file-based logging with slog.DefaultConfig() and slog.NewLoggerWithConfig()
  • Registering tools, resources, and prompts
  • Setting root paths for filesystem access
  • Using stdio transport with srv.AsStdio()
Running the Server
# Build the server example
go build -o examples/server/server examples/server/main.go

# Run the server
./examples/server/server

The server will:

  • Log messages to the logs directory (created automatically)
  • Use text format for better human readability
  • Print a brief message to the console to confirm startup

The server exposes:

  • A tool called "echo" that echoes back a message
  • A resource at "/hello" that returns a simple greeting
  • A prompt called "greeting" that formats a greeting with a name
  • Root paths set to the examples and server directories
Testing with JSON-RPC Messages

Since this is a stdio-based server, you can interact with it by sending JSON-RPC messages to its standard input. Here are some example messages you can paste into the terminal:

Initialize
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "initialize",
  "params": { "protocolVersion": "draft" }
}
Call the Echo Tool
{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "tools/call",
  "params": { "name": "echo", "args": { "message": "Hello from JSON-RPC!" } }
}
Get the Hello Resource
{
  "jsonrpc": "2.0",
  "id": 3,
  "method": "resources/read",
  "params": { "uri": "/hello" }
}
Render the Greeting Prompt
{
  "jsonrpc": "2.0",
  "id": 4,
  "method": "prompts/render",
  "params": { "name": "greeting", "args": { "name": "Example User" } }
}
List Tools
{ "jsonrpc": "2.0", "id": 5, "method": "tools/list" }

Copy and paste these messages one at a time into the terminal where the server is running. The server will process each message and return a JSON-RPC response.

Viewing Logs

The logs will be in the logs directory with filenames like:

mcp-example-2023-08-10-15-30-45.log

You can view the logs with any text editor or using commands like:

cat logs/mcp-example-*.log

Directories

Path Synopsis
cancellation
client command
Package main provides a client example for cancellation in MCP.
Package main provides a client example for cancellation in MCP.
server command
Package main provides a server example for MCP cancellation functionality.
Package main provides a server example for MCP cancellation functionality.
Package main provides an example of using gRPC transport for gomcp
Package main provides an example of using gRPC transport for gomcp
Package main provides an example of using the HTTP transport for gomcp
Package main provides an example of using the HTTP transport for gomcp
minimal
client command
server command
Package main provides an example of using the MQTT transport for gomcp
Package main provides an example of using the MQTT transport for gomcp
Package main provides an example of using the NATS transport for gomcp
Package main provides an example of using the NATS transport for gomcp
Package main demonstrates how to use the server configuration functionality to manage multiple MCP servers from a configuration file.
Package main demonstrates how to use the server configuration functionality to manage multiple MCP servers from a configuration file.
Package main provides an example of using the SSE (Server-Sent Events) transport for gomcp
Package main provides an example of using the SSE (Server-Sent Events) transport for gomcp
Package main provides an example of using stdio transport for gomcp
Package main provides an example of using stdio transport for gomcp
Package main provides an example of using UDP transport for gomcp
Package main provides an example of using UDP transport for gomcp
Package main provides an example of using Unix Socket transport for gomcp
Package main provides an example of using Unix Socket transport for gomcp
Package main provides an example of using the WebSocket transport for gomcp
Package main provides an example of using the WebSocket transport for gomcp

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL