UNPKG

hana-cli

Version:
403 lines (279 loc) โ€ข 12.5 kB
# MCP Connection Context Documentation Index ## Quick Answer to Your Question **Q: How is the MCP server getting its connection context and how do I fix it to use project-specific connections instead of install path?** **A**: Currently, the MCP server always uses connection files from the install path (`~/.hana-cli/default.json`) or user home directory. The solution is to pass project-specific context through MCP tool parameters, which the executor then applies as the working directory and environment variables when spawning the CLI. --- ## Documentation Guide This directory contains comprehensive documentation for implementing project-specific connection context in the MCP server. Choose your starting point based on your role: ### ๐ŸŽฏ Quick Start (5-10 minutes) **[visual-summary.md](./visual-summary.md)** - Problem visualization and architecture flows - Before/after diagrams - Data flow examples - Quick reference diagrams **Best for**: Getting a mental model quickly --- ### ๐Ÿ‘จโ€๐Ÿ’ป Implementation (2-3 hours) **[implementation-guide.md](./implementation-guide.md)** - Step-by-step checklist - Exact code changes with line numbers - Before/after code snippets - Testing checklist - Validation steps **Best for**: Developers ready to implement the solution --- ### ๐Ÿ“š Understanding (30-60 minutes) **[connection-context-solution.md](./connection-context-solution.md)** - Detailed walkthrough - Current flow explanation with code - Key code locations - Step-by-step implementation walkthrough - Real usage examples - Files to modify summary **Best for**: Understanding the complete solution approach --- ### ๐Ÿ—๏ธ Architecture (1 hour) **[connection-context-analysis.md](./connection-context-analysis.md)** - Deep architectural analysis - Multiple implementation options - Pros/cons of each approach - Architecture diagrams - Security considerations - Future enhancements - Migration strategies **Best for**: Architects and decision makers --- ### ๐Ÿ“‹ Historical Reference **[implementation-complete.md](./implementation-complete.md)** - Implementation status snapshot (Feb 2026) - Historical snapshot of what was implemented - Specific code changes made - Build status from Feb 2026 - Usage examples from initial implementation **Note**: This is a historical document. For current implementation, use the guides above. --- ## Related Documentation **MCP Server Core:** - [Architecture](./architecture.md) - Overall MCP technical architecture - [Server Usage](./server-usage.md) - Running and configuring the MCP server - [Connection Guide](./connection-guide.md) - General connection configuration **Other Features:** - [MCP Index](./index.md) - Main MCP documentation hub - [Server Updates](./server-updates.md) - Recent MCP server enhancements --- ## How to Use These Documents ### For Quick Understanding (15 minutes) 1. Read **visual-summary.md** - Get mental model 2. Skim **implementation-guide.md** - See what needs to change ### For Implementation (2-3 hours) 1. Read **implementation-guide.md** - Follow step-by-step 2. Reference **connection-context-solution.md** - For detailed explanations 3. Reference **visual-summary.md** - If confused about flow ### For Design Review (1 hour) 1. Read **connection-context-analysis.md** - Understand all options 2. Review **visual-summary.md** - Verify architecture 3. Check **implementation-guide.md** - Assess effort --- ## The Problem in One Diagram ```bash โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ AI Agent working on Project A โ”‚ โ”‚ โ†’ Asks: "List tables in my database" โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ†“ MCP Server gets request โ†“ Spawns CLI with: โ€ข cwd = /install/path (WRONG!) โ€ข env = parent environment (NO PROJECT CONTEXT) โ†“ CLI looks for connection file โ€ข Searches from /install/path upward โ€ข Finds ~/.hana-cli/default.json โ€ข Connects to DEFAULT DATABASE โ†“ โœ— WRONG! Should connect to Project A's database โœ— Agent gets data from wrong database โœ— All projects use same default connection ``` ## The Solution in One Diagram ```bash โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ AI Agent working on Project A โ”‚ โ”‚ โ†’ Asks: "List tables in my database" โ”‚ โ”‚ + Provides: __projectContext.projectPath=/proj/a โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ†“ MCP Server extracts context & spawns CLI with: โ€ข cwd = /proj/a (โœ“ PROJECT DIRECTORY!) โ€ข env.HANA_CLI_PROJECT_PATH = /proj/a โ€ข env.HANA_CLI_CONN_FILE = ".env" โ†“ CLI looks for connection file โ€ข Searches from /proj/a upward โ€ข Finds /proj/a/.env โ€ข Connects to PROJECT A'S DATABASE โ†“ โœ“ CORRECT! Connects to Project A's database โœ“ Agent gets data from correct database โœ“ Each project uses its own connection ``` --- ## Current System Flow ```bash MCP Tool Call โ†“ Tool Handler (index.ts line 1325) โ†“ executeCommand(commandName, args) โ† No project context! โ†“ spawn('node', [cli.js], { cwd: /install/path โ† HARDCODED! env: process.env โ† Parent env, no project context }) โ†“ CLI Process โ†“ Connection Resolution (connections.js) โ€ข Starts from cwd = /install/path โ€ข Finds ~/.hana-cli/default.json โ€ข Uses default database โœ— ``` ## New System Flow ```bash MCP Tool Call with __projectContext โ†“ Tool Handler (index.ts) โ† UPDATED โ”œโ”€ Extract: context = args.__projectContext โ”œโ”€ Clean args (remove context key) โ””โ”€ Pass: executeCommand(cmd, args, context) โ† NEW! โ†“ executeCommand(commandName, args, context) โ† UPDATED โ”œโ”€ Build env with: HANA_CLI_PROJECT_PATH, etc. โ”œโ”€ Set cwd = context.projectPath โ† DYNAMIC! โ””โ”€ spawn('node', [cli.js], {env, cwd}) โ†“ CLI Process โ†“ Connection Resolution โ† UPDATED โ€ข Checks: process.env.HANA_CLI_PROJECT_PATH โ€ข Starts from cwd = /project/path โ€ข Finds /project/path/.env โ€ข Uses project database โœ“ ``` --- ## Step-by-Step Implementation Priority ### Phase 1: Core Implementation (HIGH PRIORITY) **Time**: ~4 hours | **Complexity**: Medium 1. โœ๏ธ Create `connection-context.ts` interface 2. โœ๏ธ Update `executor.ts` - add context param, build env, set cwd 3. โœ๏ธ Update `index.ts` - extract context, update schemas 4. โœ๏ธ Update `connections.js` - check env vars 5. โœ… Test basic functionality ### Phase 2: Enhancement (MEDIUM PRIORITY) **Time**: ~2-3 hours | **Complexity**: Low-Medium 1. ๐Ÿ“‹ Add connection management tools 2. ๐Ÿ“‹ Add project auto-detection 3. ๐Ÿ“‹ Add connection switching within conversation ### Phase 3: Polish (LOW PRIORITY) **Time**: ~2 hours | **Complexity**: Low 1. ๐Ÿ”’ Add security validations 2. ๐Ÿ“– Update documentation 3. ๐Ÿงช Add comprehensive tests --- ## Key Technical Insights ### Why It Currently Doesn't Work 1. **Hardcoded CWD**: `executor.ts` line 270 has `cwd: join(__dirname, '..', '..')` 2. **No Context Passing**: Connection context never flows from MCP to CLI 3. **Parent Environment Only**: Spawned process inherits parent env, no project override 4. **Search Order**: `connections.js` searches up from cwd, which is always install path ### Why The Solution Works 1. **Dynamic CWD**: Pass `context.projectPath` as cwd to spawn 2. **Environment Signaling**: Use env vars (HANA_CLI_PROJECT_PATH, etc.) to signal project context 3. **CLI Auto-Detection**: CLI looks for these env vars first, changes directory if set 4. **Correct Search Order**: Connection resolution now starts from project directory --- ## Technical Debt & Future Work ### Potential Enhancements - **Connection Registry**: Store project connections for reuse - **Session Context**: Set context once, use for multiple commands - **Auto-Detection**: Automatically detect project from CLI context - **Secure Vault**: Encrypted credential storage - **Connection Profiles**: Named connection sets for different environments ### Security Improvements - Path validation/normalization - Credential encryption - Audit logging for connections - Access control per project --- ## Questions Answered **Q: Where does the MCP server currently get credentials?** - From `~/.hana-cli/default.json` or `default-env.json` in install path **Q: Why doesn't it use project's .env?** - Because the CLI is spawned with `cwd = /install/path`, not project path **Q: How do I send project context to MCP?** - Add `__projectContext` object to any tool parameter **Q: Will existing integrations break?** - No! `__projectContext` is optional, backward compatible **Q: What's the simplest way to implement this?** - Follow **implementation-guide.md** step 1-5 **Q: How do I test if it works?** - See validation checklist in **implementation-guide.md** --- ## Document Recommendation by Role ### ๐Ÿ‘จโ€๐Ÿ’ผ Project Manager / Decision Maker โ†’ Start with: **visual-summary.md** โ†’ Decision: 15 minutes, understand scope and benefits ### ๐Ÿ‘จโ€๐Ÿ’ป Developer / Implementation โ†’ Start with: **implementation-guide.md** โ†’ Implement: 2-3 hours, line-by-line code changes ### ๐Ÿ—๏ธ Architect / Technical Lead โ†’ Start with: **connection-context-analysis.md** โ†’ Design: 1 hour, evaluate options and approach ### ๐Ÿ”Ž Code Reviewer โ†’ Check against: **implementation-guide.md** checklist โ†’ Review: Cross-verify all modified files ### ๐Ÿค” Understanding Decision โ†’ Read in order: 1. **visual-summary.md** (5 min) 2. **connection-context-solution.md** (15 min) 3. **implementation-guide.md** (10 min) โ†’ Total: 30 minutes for complete understanding --- ## Success Criteria After implementation, you should be able to: - โœ… Pass project path to MCP tools via `__projectContext.projectPath` - โœ… Have CLI execute from that project directory - โœ… Connection files found in project directory first - โœ… Multiple projects use different databases in one conversation - โœ… Backward compatible with existing integrations - โœ… No breaking changes to any APIs --- ## Support Matrix | Issue | Solution | Document | | -------- | -------- | -------- | | "What's the problem?" | Start here | visual-summary.md | | "How do I fix it?" | Code changes | implementation-guide.md | | "Walk me through it" | Detailed explanation | connection-context-solution.md | | "What about X?" | Design considerations | connection-context-analysis.md | | "Show me diagrams" | Visual explanations | visual-summary.md | | "I need the code" | Exact changes | implementation-guide.md | --- ## Next Steps 1. **Choose your starting document** based on your role above 2. **Understand the problem** using diagrams and flows 3. **Plan implementation** using the guide 4. **Execute changes** following the step-by-step process 5. **Test thoroughly** using provided test matrix 6. **Deploy with confidence** - fully backward compatible! --- ## Quick Links to Documents | Document | Purpose | Time | | -------- | ------- | ---- | | ๐Ÿ“Š [visual-summary.md](./visual-summary.md) | Diagrams & quick overview | 15 min | | ๐Ÿ› ๏ธ [implementation-guide.md](./implementation-guide.md) | Code changes & checklist | 2-3 hrs | | ๐Ÿ“ [connection-context-solution.md](./connection-context-solution.md) | Detailed walkthrough | 30 min | | ๐ŸŽฏ [connection-context-analysis.md](./connection-context-analysis.md) | Full analysis & options | 1 hour | --- ## Questions? If you have questions about: - **The problem**: Check visual-summary.md diagrams - **The implementation**: Check implementation-guide.md code sections - **The design choices**: Check connection-context-analysis.md options - **The flow**: Check connection-context-solution.md walkthrough All documents include examples, diagrams, and detailed explanations for different learning styles.