Skip to content

feat: enhance go:// protocol to support local paths - #487

Merged
JAORMX merged 3 commits into
mainfrom
feature/go-protocol-local-paths
May 28, 2025
Merged

JAORMX merged 3 commits into
mainfrom
feature/go-protocol-local-paths

Conversation

@JAORMX

@JAORMX JAORMX commented May 28, 2025 •

Copy link
Copy Markdown
Collaborator

Summary

This PR enhances the go:// protocol to detect and handle local paths, allowing users to easily containerize and run local Go MCP servers with a simplified, Docker-native approach.

Key Features

🎯 Local Path Detection

  • Detects paths starting with ./, ../, /, or just .
  • Uses local directory directly as Docker build context (no file copying)
  • Respects existing .dockerignore files for intelligent file filtering

🐳 Docker-Native Approach

  • Simplified Architecture: Uses local directory directly as build context instead of copying files
  • Leverages .dockerignore: Relies on Docker's built-in file filtering instead of custom skip logic
  • Dockerfile Safety: Detects existing Dockerfiles and uses them instead of overwriting
  • Clean Builds: Temporary generated Dockerfiles are marked and cleaned up automatically

🔧 Enhanced Image Naming

  • Handles local paths with valid Docker image names
  • Converts problematic characters and ensures valid naming conventions
  • Generates unique timestamps for image versioning

Implementation Details

Refactored Architecture

The implementation has been refactored into focused, maintainable functions:

  • setupBuildContext() - handles local vs temp directory setup
  • writeDockerfile() - writes Dockerfile with existence checks
  • writeCACertificate() - handles CA certificate files
  • generateImageName() - creates unique image names

Usage Examples

# Run from a relative path
thv run go://./cmd/my-mcp-server

# Run from current directory
cd my-go-project && thv run go://.

# Run from absolute path
thv run go:///path/to/project

Benefits

✅ Simpler & More Reliable: No complex file copying or filtering logic
✅ Respects User Intent: Uses existing .dockerignore files and doesn't overwrite Dockerfiles
✅ Better Performance: No file copying overhead for local builds
✅ Docker Native: Leverages Docker's built-in capabilities
✅ Cleaner Code: Smaller, focused functions that are easier to test and maintain

Backward Compatibility

✅ Maintains full backward compatibility with existing go:// usage for remote packages

Testing & Quality

  • ✅ Comprehensive unit tests covering local path detection, image naming, and protocol scheme parsing
  • ✅ All existing tests continue to pass
  • ✅ Linting compliance with 0 issues
  • ✅ Code refactoring for better maintainability and reduced complexity

Files Modified

  • pkg/runner/protocol.go - Core implementation with simplified architecture
  • pkg/runner/protocol_test.go - Updated test suite
  • pkg/container/templates/templates.go - Added IsLocalPath field
  • pkg/container/templates/go.tmpl - Conditional source copying for local paths
  • README.md - Updated documentation with local path examples
  • cmd/thv/app/run.go - Updated help text

This approach is cleaner, more reliable, and leverages Docker's native .dockerignore functionality for file filtering while maintaining the same user experience.

@JAORMX
JAORMX force-pushed the feature/go-protocol-local-paths branch 2 times, most recently from 35126d3 to a582093 Compare May 28, 2025 12:26
Comment thread pkg/runner/protocol.go Resolved
@JAORMX
JAORMX force-pushed the feature/go-protocol-local-paths branch from b04844a to 796a719 Compare May 28, 2025 13:23
JAORMX added 3 commits May 28, 2025 18:01
- Add local path detection for go:// protocol (./path, ../path, /, .)
- Implement source code copying for local Go projects
- Update Go template to conditionally copy local source files
- Add IsLocalPath field to TemplateData struct
- Enhance image name generation for local paths
- Add comprehensive unit tests (295 lines) covering:
  - Local path detection logic
  - File copying with smart skip logic
  - Image name generation
  - Protocol scheme parsing
  - Template data creation
- Update documentation with local path usage examples
- Refactor HandleProtocolScheme to reduce cyclomatic complexity
- Fix all linting issues and maintain code quality standards

This allows users to run local Go MCP servers with commands like:
- thv run go://./cmd/server
- thv run go://.
- thv run go:///absolute/path

Maintains backward compatibility with existing remote package usage.

Signed-off-by: Juan Antonio Osorio <[email protected]>
- Add filepath.Match support for wildcard patterns like *.tmp and *.log
- Remove .dockerignore from skip list to respect Docker build context rules
- Add comprehensive test cases for wildcard pattern matching
- Maintain backward compatibility with exact and prefix matching

Addresses PR feedback about wildcard handling in file skip logic.
- Use local directory directly as Docker build context instead of copying files
- Remove complex file copying and skip logic in favor of Docker's native .dockerignore
- Check for existing Dockerfile and avoid overwriting it
- Add marker comments to identify generated Dockerfiles for cleanup
- Refactor buildImageFromTemplate into smaller, focused functions:
  - setupBuildContext: handles local vs temp directory setup
  - writeDockerfile: writes Dockerfile with existence checks
  - writeCACertificate: handles CA certificate files
  - generateImageName: creates unique image names
- Remove unused functions: copyLocalSource, shouldSkipPath, copyFile
- Update tests to remove tests for deleted functionality
- Fix all linting issues

This approach is cleaner, more reliable, and leverages Docker's built-in
.dockerignore functionality for file filtering.
@JAORMX
JAORMX force-pushed the feature/go-protocol-local-paths branch from 6895778 to d49ba75 Compare May 28, 2025 15:01
@JAORMX
JAORMX merged commit 230d951 into main May 28, 2025
@JAORMX
JAORMX deleted the feature/go-protocol-local-paths branch May 28, 2025 17:26
@jhrozek jhrozek mentioned this pull request Sep 25, 2026
3 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants