Thanks to visit codestin.com
Credit goes to github.com

Skip to content

malnick/mcp-test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MCP Filesystem Server and Client

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.

Features

  • MCP Server: Implements the Model Context Protocol to provide filesystem tools
  • Filesystem Tools:
    • read_file: Read contents of files in /tmp/mcp
    • list_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/mcp directory for safety

Prerequisites

  • Go 1.21 or later
  • OpenAI API key

Installation

  1. Clone the repository:
git clone <repository-url>
cd mcp-test
  1. Install dependencies:
make deps
  1. Build the server and client:
make build

Usage

Setting up the OpenAI API Key

Set your OpenAI API key as an environment variable:

export OPENAI_API_KEY="your-openai-api-key-here"

Running the Client

The client will automatically start the MCP server and provide an interactive chat interface:

make run

Or run the client directly:

./client

Interactive Usage

Once 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

Manual Testing

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.txt

Architecture

MCP Server (server/main.go)

The server implements the Model Context Protocol and provides two tools:

  1. read_file: Reads file contents from /tmp/mcp

    • Parameters: path (string) - relative path to file
    • Returns: File contents as text
  2. list_files: Lists directory contents in /tmp/mcp

    • Parameters: path (string, optional) - relative path to directory
    • Returns: List of files and directories

MCP Client (client/main.go)

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

Security Considerations

  • 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

Development

Building Individual Components

# Build only the server
make server

# Build only the client
make client

Cleaning Build Artifacts

make clean

Project Structure

mcp-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

Troubleshooting

Common Issues

  1. "OPENAI_API_KEY environment variable is required"

    • Make sure you've set the environment variable before running the client
  2. "Failed to start server"

    • Ensure the server binary was built successfully with make build
  3. "File not found" errors

    • Make sure the files exist in /tmp/mcp directory
    • Check that the path is relative to /tmp/mcp
  4. Permission errors

    • Ensure you have read permissions for files in /tmp/mcp
    • The client will create /tmp/mcp if it doesn't exist

Debug Mode

To see more detailed logging, you can modify the log level in the source code or add debug prints.

License

This project is provided as-is for educational and development purposes.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published