diff options
| author | Paul Buetow <paul@buetow.org> | 2026-02-10 19:28:27 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-02-10 19:28:27 +0200 |
| commit | 5551695f3b0d10c9a22cfacdb10c2cf7bd572421 (patch) | |
| tree | 282611eacf1fd4c38d54d5cea87decdf2b1cbdb7 /internal/mcp/transport.go | |
| parent | ec745129258ae800065e302a2a40b54488cbca08 (diff) | |
Add MCP server implementation with comprehensive test coverage
Implements a full Model Context Protocol (MCP) server for managing and serving prompts
to LLM applications. The server provides CRUD operations for prompts with automatic
backups and template rendering support.
Key additions:
- cmd/hexai-mcp-server: Main MCP server binary entrypoint
- internal/hexaimcp: Server orchestrator with configuration and setup
- internal/mcp: Core MCP protocol implementation (JSON-RPC 2.0)
- internal/promptstore: Prompt storage with JSONL backend and automatic backups
- Comprehensive test suites achieving 80%+ coverage for all MCP packages
- Magefile targets for building and installing the MCP server
- Complete documentation for setup, API, prompts, and backups
Test coverage:
- internal/hexaimcp: 84.3%
- internal/mcp: 80.3%
- internal/promptstore: 81.2%
- Overall project: 81.5%
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'internal/mcp/transport.go')
| -rw-r--r-- | internal/mcp/transport.go | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/internal/mcp/transport.go b/internal/mcp/transport.go new file mode 100644 index 0000000..aba416a --- /dev/null +++ b/internal/mcp/transport.go @@ -0,0 +1,69 @@ +// Summary: MCP transport utilities for reading and writing JSON-RPC messages with Content-Length framing. +package mcp + +import ( + "encoding/json" + "fmt" + "io" + "net/textproto" + "strconv" + "strings" +) + +// readMessage reads a Content-Length framed JSON-RPC message from the input stream. +// Returns the raw JSON bytes. Follows LSP/JSON-RPC framing convention. +func (s *Server) readMessage() ([]byte, error) { + tp := textproto.NewReader(s.in) + var contentLength int + for { + line, err := tp.ReadLine() + if err != nil { + return nil, err + } + if line == "" { // end of headers + break + } + parts := strings.SplitN(line, ":", 2) + if len(parts) != 2 { + continue + } + key := strings.TrimSpace(strings.ToLower(parts[0])) + val := strings.TrimSpace(parts[1]) + switch key { + case "content-length": + n, err := strconv.Atoi(val) + if err != nil { + return nil, fmt.Errorf("invalid Content-Length: %v", err) + } + contentLength = n + } + } + if contentLength <= 0 { + return nil, fmt.Errorf("missing or invalid Content-Length") + } + buf := make([]byte, contentLength) + if _, err := io.ReadFull(s.in, buf); err != nil { + return nil, err + } + return buf, nil +} + +// writeMessage writes a JSON-RPC response with Content-Length framing. +// Thread-safe via mutex lock. +func (s *Server) writeMessage(v any) error { + s.outMu.Lock() + defer s.outMu.Unlock() + + data, err := json.Marshal(v) + if err != nil { + return fmt.Errorf("marshal error: %w", err) + } + header := fmt.Sprintf("Content-Length: %d\r\n\r\n", len(data)) + if _, err := io.WriteString(s.out, header); err != nil { + return fmt.Errorf("write header error: %w", err) + } + if _, err := s.out.Write(data); err != nil { + return fmt.Errorf("write body error: %w", err) + } + return nil +} |
