svrnty-mcp-server/samples/CodexMcpServer/Tools/GetDocumentTool.cs
Svrnty 516e1479c6 docs: comprehensive AI coding assistant research and MCP-first implementation plan
Research conducted on modern AI coding assistants (Cursor, GitHub Copilot, Cline,
Aider, Windsurf, Replit Agent) to understand architecture patterns, context management,
code editing workflows, and tool use protocols.

Key Decision: Pivoted from building full CLI (40-50h) to validation-driven MCP-first
approach (10-15h). Build 5 core CODEX MCP tools that work with ANY coding assistant,
validate adoption over 2-4 weeks, then decide on full CLI if demand proven.

Files:
- research/ai-systems/modern-coding-assistants-architecture.md (comprehensive research)
- research/ai-systems/codex-coding-assistant-implementation-plan.md (original CLI plan, preserved)
- research/ai-systems/codex-mcp-tools-implementation-plan.md (approved MCP-first plan)
- ideas/registry.json (updated with approved MCP tools proposal)

Architech Validation: APPROVED with pivot to MCP-first approach
Human Decision: Approved (pragmatic validation-driven development)

Next: Begin Phase 1 implementation (10-15 hours, 5 core MCP tools)

🤖 Generated with CODEX Research System

Co-Authored-By: The Archivist <archivist@codex.svrnty.io>
Co-Authored-By: The Architech <architech@codex.svrnty.io>
Co-Authored-By: Mathias Beaulieu-Duncan <mat@svrnty.io>
2025-10-22 21:00:34 -04:00

113 lines
3.3 KiB
C#

using System;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using OpenHarbor.MCP.Core;
namespace CodexMcpServer.Tools;
/// <summary>
/// MCP tool for retrieving a specific CODEX document by ID.
/// Calls CODEX API GET /api/documents/{id} endpoint.
/// </summary>
public class GetDocumentTool : IMcpTool
{
private readonly HttpClient _httpClient;
private static readonly JsonDocument _schema = JsonDocument.Parse("""
{
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "Document ID to retrieve"
}
},
"required": ["id"]
}
""");
public GetDocumentTool(HttpClient httpClient)
{
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
// Set base address if not already set
if (_httpClient.BaseAddress == null)
{
_httpClient.BaseAddress = new Uri("http://localhost:5050");
}
}
/// <inheritdoc/>
public string Name => "get_document";
/// <inheritdoc/>
public string Description => "Retrieve a specific CODEX document by ID. Returns complete document details including metadata, content, and sections.";
/// <inheritdoc/>
public JsonDocument Schema => _schema;
/// <inheritdoc/>
public async Task<JsonDocument> ExecuteAsync(JsonDocument? arguments)
{
try
{
// Validate arguments
if (arguments == null)
{
return CreateErrorResponse("Arguments cannot be null. Expected {\"id\": \"document_id\"}");
}
var root = arguments.RootElement;
if (!root.TryGetProperty("id", out var idElement))
{
return CreateErrorResponse("Missing required parameter 'id'");
}
var id = idElement.GetString();
if (string.IsNullOrWhiteSpace(id))
{
return CreateErrorResponse("Document ID cannot be empty");
}
// Call CODEX API
var response = await _httpClient.GetAsync($"/api/documents/{id}");
if (!response.IsSuccessStatusCode)
{
var errorContent = await response.Content.ReadAsStringAsync();
return CreateErrorResponse(
$"CODEX API returned error: {response.StatusCode}. {errorContent}"
);
}
// Parse and return response
var responseContent = await response.Content.ReadAsStringAsync();
try
{
return JsonDocument.Parse(responseContent);
}
catch (JsonException ex)
{
return CreateErrorResponse($"Failed to parse CODEX API response: {ex.Message}");
}
}
catch (HttpRequestException ex)
{
return CreateErrorResponse($"HTTP request failed: {ex.Message}");
}
catch (Exception ex)
{
return CreateErrorResponse($"Unexpected error: {ex.Message}");
}
}
private static JsonDocument CreateErrorResponse(string message)
{
var error = new
{
error = message
};
return JsonDocument.Parse(JsonSerializer.Serialize(error));
}
}