This project implements a Model Context Protocol (MCP) server in Go that provides filesystem access to files in /tmp/mcp, and a client that uses OpenAI's GPT-4o-mini model to interact with the filesystem tools.
- MCP Server: Implements the Model Context Protocol to provide filesystem tools
- Filesystem Tools:
read_file: Read contents of files in/tmp/mcplist_files: List files and directories in/tmp/mcp
- AI Client: Uses OpenAI GPT-4o-mini to intelligently interact with filesystem tools
- Security: Restricted to
/tmp/mcpdirectory for safety
- Go 1.21 or later
- OpenAI API key
- Clone the repository:
git clone <repository-url>
cd mcp-test- Install dependencies:
make deps- Build the server and client:
make buildSet your OpenAI API key as an environment variable:
export OPENAI_API_KEY="your-openai-api-key-here"The client will automatically start the MCP server and provide an interactive chat interface:
make runOr run the client directly:
./clientOnce the client is running, you can interact with it using natural language:
> List all files in the directory
> Read the contents of file.txt
> What files are in the subdirectory?
> Show me the contents of config.json
You can also test the server directly by creating some test files:
# Create the test directory
mkdir -p /tmp/mcp
# Create some test files
echo "Hello, World!" > /tmp/mcp/test.txt
echo '{"name": "test", "value": 42}' > /tmp/mcp/config.json
mkdir /tmp/mcp/subdir
echo "Subdirectory content" > /tmp/mcp/subdir/file.txtThe server implements the Model Context Protocol and provides two tools:
-
read_file: Reads file contents from
/tmp/mcp- Parameters:
path(string) - relative path to file - Returns: File contents as text
- Parameters:
-
list_files: Lists directory contents in
/tmp/mcp- Parameters:
path(string, optional) - relative path to directory - Returns: List of files and directories
- Parameters:
The client:
- Starts the MCP server as a subprocess
- Communicates with the server using JSON-RPC over stdin/stdout
- Uses OpenAI GPT-4o-mini to interpret user requests and call appropriate tools
- Provides an interactive chat interface
- The server is restricted to only access files within
/tmp/mcp - Path traversal attacks are prevented by checking that all paths start with
/tmp/mcp - The server runs as a subprocess and can be easily terminated
# Build only the server
make server
# Build only the client
make clientmake cleanmcp-test/
├── server/
│ └── main.go # MCP server implementation
├── client/
│ └── main.go # MCP client with OpenAI integration
├── go.mod # Go module dependencies
├── Makefile # Build and run commands
└── README.md # This file
-
"OPENAI_API_KEY environment variable is required"
- Make sure you've set the environment variable before running the client
-
"Failed to start server"
- Ensure the server binary was built successfully with
make build
- Ensure the server binary was built successfully with
-
"File not found" errors
- Make sure the files exist in
/tmp/mcpdirectory - Check that the path is relative to
/tmp/mcp
- Make sure the files exist in
-
Permission errors
- Ensure you have read permissions for files in
/tmp/mcp - The client will create
/tmp/mcpif it doesn't exist
- Ensure you have read permissions for files in
To see more detailed logging, you can modify the log level in the source code or add debug prints.
This project is provided as-is for educational and development purposes.