Skip to content

Commit e9dc2ce

Browse files
authored
fix: Add instructions + fix meta-resources (#35)
* docs: fix bob instructions@ * feat: enrich resource meta-data definitions * fix: mcp resource description * fix: mcp resource description * feature: explicit instructions to llm to use index first before searching * fix: path with uvx * fix: path with uvx
1 parent 367144a commit e9dc2ce

File tree

2 files changed

+104
-74
lines changed

2 files changed

+104
-74
lines changed

static/instructions.md

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ This MCP server is designed to help you leverage Terraform IBM Modules (TIM) to
1515

1616
## Purpose of this MCP Server
1717

18-
The main purpose of this MCP server is to help generate terraform-based compositions of modules. It provides tools to:
18+
The main purpose of this MCP server is to help generate terraform-based compositions of modules. It provides tools and resources to:
1919

20-
1. Search for relevant Terraform IBM Modules
21-
2. Discover available examples and repository structure
22-
3. Get detailed information about modules, including inputs, outputs, and examples
23-
4. Access example code and specific files to understand how to use and combine modules
24-
5. Generate complete, working Terraform configurations
20+
1. Browse the module index for a high-level overview of available modules
21+
2. Search for specific Terraform IBM Modules when needed
22+
3. Discover available examples and repository structure
23+
4. Get detailed information about modules, including inputs, outputs, and examples
24+
5. Access example code and specific files to understand how to use and combine modules
25+
6. Generate complete, working Terraform configurations
2526

2627
## Architectural Best Practices
2728

@@ -44,10 +45,11 @@ The server supports two distinct workflows based on user intent:
4445

4546
Keywords to detect this intent: "example", "sample", "deploy", "show me", "simple"
4647

47-
1. **`search_modules`** → Find relevant modules
48-
2. **`get_module_details`** → Understand module capabilities using module ID from search results
49-
3. **`list_content`** → Check what examples are available for the module
50-
4. **`get_content`** → Fetch example Terraform files (main.tf, provider.tf, version.tf)
48+
1. First, check the **`catalog://terraform-ibm-modules-index`** resource to get a broad, high-level picture of available modules
49+
2. If needed, use **`search_modules`** to find more specific modules (optional if the index provides enough information)
50+
3. Use **`get_module_details`** to understand module capabilities using module ID from the index or search results
51+
4. Use **`list_content`** to check what examples are available for the module
52+
5. Use **`get_content`** to fetch example Terraform files (main.tf, provider.tf, version.tf)
5153

5254
The example files provide valuable insights:
5355
- **Main configuration file**: Shows how to use and combine the module with others
@@ -62,14 +64,22 @@ Note: File names may vary (e.g., main.tf, provider.tf, version.tf, variables.tf,
6264

6365
Keywords to detect this intent: "create", "build", "inputs", "outputs", "develop"
6466

65-
1. **`search_modules`** → Find relevant modules
66-
2. **`get_module_details`** → Understand inputs/outputs/interface using module ID from search results
67-
3. **`list_content`** → Explore available examples and structure
68-
4. **`get_content`** → Fetch example files to understand usage patterns and provider setup
67+
1. First, check the **`catalog://terraform-ibm-modules-index`** resource to get a broad, high-level picture of available modules
68+
2. If needed, use **`search_modules`** to find more specific modules (optional if the index provides enough information)
69+
3. Use **`get_module_details`** to understand inputs/outputs/interface using module ID from the index or search results
70+
4. Use **`list_content`** to explore available examples and structure
71+
5. Use **`get_content`** to fetch example files to understand usage patterns and provider setup
6972

70-
## Tool Usage Tips
73+
## Resource and Tool Usage Tips
74+
75+
### Module Index Resource
76+
- Start with the module index resource (`catalog://terraform-ibm-modules-index`) to get a broad, high-level picture of available modules
77+
- Use the index to understand the overall ecosystem of available modules before diving into specific searches
78+
- The index provides a comprehensive overview that can help identify the most relevant modules for your needs
79+
- Only proceed to search if the index doesn't provide enough specific information
7180

7281
### Search Strategy
82+
- Use search only when you need more specific results than what the index provides
7383
- Use specific terms rather than generic ones (e.g., "vpc" better than "network")
7484
- Be specific in requests to minimize context usage and API calls
7585
- Consider download counts as indicators of module quality and maintenance
@@ -88,11 +98,12 @@ Keywords to detect this intent: "create", "build", "inputs", "outputs", "develop
8898

8999
## Optimization Principles
90100

91-
1. **Be specific in requests** to minimize context usage and API calls
92-
2. **Start with narrow scope** (specific files/paths), broaden only if needed
93-
3. **Exclude test files by default**: `[".*test.*", ".*\\.tftest$", ".*_test\\..*"]`
94-
4. **For examples, prefer single targeted example** over fetching all examples
95-
5. **Avoid multiple searches** unless comparing approaches
101+
1. **Start with the module index resource** to get a comprehensive overview before specific searches
102+
2. **Be specific in requests** to minimize context usage and API calls
103+
3. **Start with narrow scope** (specific files/paths), broaden only if needed
104+
4. **Exclude test files by default**: `[".*test.*", ".*\\.tftest$", ".*_test\\..*"]`
105+
5. **For examples, prefer single targeted example** over fetching all examples
106+
6. **Avoid multiple searches** unless comparing approaches
96107

97108
## Example Workflows
98109

@@ -103,8 +114,8 @@ Keywords to detect this intent: "create", "build", "inputs", "outputs", "develop
103114

104115
**Workflow:**
105116
```
106-
1. search_modules("vpc")
107-
# Returns: terraform-ibm-modules/landing-zone-vpc/ibm/8.4.0 (among others)
117+
1. Check catalog://terraform-ibm-modules-index
118+
# Browse the index to identify relevant VPC modules
108119
2. get_module_details("terraform-ibm-modules/landing-zone-vpc/ibm/8.4.0")
109120
# Understand module capabilities, inputs, outputs
110121
3. list_content("terraform-ibm-modules/landing-zone-vpc/ibm/8.4.0")
@@ -120,8 +131,8 @@ Keywords to detect this intent: "create", "build", "inputs", "outputs", "develop
120131

121132
**Workflow:**
122133
```
123-
1. search_modules("vpc")
124-
# Returns: terraform-ibm-modules/landing-zone-vpc/ibm/8.4.0 (among others)
134+
1. Check catalog://terraform-ibm-modules-index
135+
# Browse the index to identify relevant VPC modules
125136
2. get_module_details("terraform-ibm-modules/landing-zone-vpc/ibm/8.4.0")
126137
# Get detailed inputs, outputs, and module interface
127138
3. list_content("terraform-ibm-modules/landing-zone-vpc/ibm/8.4.0")
@@ -138,12 +149,12 @@ Keywords to detect this intent: "create", "build", "inputs", "outputs", "develop
138149

139150
**Workflow:**
140151
```
141-
1. search_modules("vpc")
142-
# Returns: terraform-ibm-modules/landing-zone-vpc/ibm/8.4.0, etc.
152+
1. Check catalog://terraform-ibm-modules-index
153+
# Browse the index to identify relevant modules for VPC, security groups, and clusters
143154
2. search_modules("security group")
144-
# Returns: terraform-ibm-modules/security-group/ibm/2.7.0, etc.
155+
# Optional: Search for specific security group modules if needed
145156
3. search_modules("cluster")
146-
# Returns: terraform-ibm-modules/base-ocp-vpc/ibm/3.62.0, etc.
157+
# Optional: Search for specific cluster modules if needed
147158
4. get_module_details("terraform-ibm-modules/landing-zone-vpc/ibm/8.4.0")
148159
5. get_module_details("terraform-ibm-modules/security-group/ibm/2.7.0")
149160
6. get_module_details("terraform-ibm-modules/base-ocp-vpc/ibm/3.62.0")
@@ -159,8 +170,8 @@ Keywords to detect this intent: "create", "build", "inputs", "outputs", "develop
159170

160171
**Workflow:**
161172
```
162-
1. search_modules("vpc")
163-
# Returns: terraform-ibm-modules/landing-zone-vpc/ibm/8.4.0, etc.
173+
1. Check catalog://terraform-ibm-modules-index
174+
# Browse the index to identify relevant VPC modules
164175
2. get_module_details("terraform-ibm-modules/landing-zone-vpc/ibm/8.4.0")
165176
# Understand what the module does and its capabilities
166177
3. list_content("terraform-ibm-modules/landing-zone-vpc/ibm/8.4.0")
@@ -176,8 +187,8 @@ Keywords to detect this intent: "create", "build", "inputs", "outputs", "develop
176187

177188
**Workflow:**
178189
```
179-
1. search_modules("vpc")
180-
# Returns: terraform-ibm-modules/landing-zone-vpc/ibm/8.4.0, etc.
190+
1. Check catalog://terraform-ibm-modules-index
191+
# Browse the index to identify relevant VPC modules
181192
2. get_module_details("terraform-ibm-modules/landing-zone-vpc/ibm/8.4.0")
182193
# Understand module interface
183194
3. list_content("terraform-ibm-modules/landing-zone-vpc/ibm/8.4.0")
@@ -249,6 +260,11 @@ When generating Terraform configurations:
249260
- Only use IBM Cloud Terraform provider resources when no appropriate module exists
250261
- Gain insights from example code to understand when to use modules vs provider resources
251262

263+
### Module Discovery
264+
- **Start with the module index resource** to get a comprehensive overview of available modules
265+
- Use search only when you need more specific information than what the index provides
266+
- The index gives you a broad, high-level picture that helps identify the most relevant modules
267+
252268
### Search Strategy
253269
- Use specific, targeted search terms
254270
- Limit results appropriately to avoid information overload

tim_mcp/server.py

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import json
99
import time
10+
import textwrap
1011
from pathlib import Path
1112
from typing import Any
1213

@@ -30,33 +31,48 @@
3031
logger = get_logger(__name__)
3132

3233

33-
def _load_instructions() -> str:
34-
"""Load instructions from the static instructions file."""
34+
def _find_static_file(filename: str) -> Path:
35+
"""
36+
Find a file in the static directory, checking both packaged and development locations.
37+
38+
Args:
39+
filename: Name of the file to find in the static directory
40+
41+
Returns:
42+
Path object to the found file
43+
44+
Raises:
45+
FileNotFoundError: If the file cannot be found in either location
46+
"""
3547
# First try the packaged location (when installed via pip/uvx)
36-
packaged_path = Path(__file__).parent / "static" / "instructions.md"
48+
packaged_path = Path(__file__).parent / "static" / filename
3749
# Then try the development location (when running from source)
38-
dev_path = Path(__file__).parent.parent / "static" / "instructions.md"
39-
40-
for instructions_path in [packaged_path, dev_path]:
41-
if instructions_path.exists():
42-
try:
43-
return instructions_path.read_text(encoding="utf-8")
44-
except Exception as e:
45-
logger.error(
46-
f"Error reading instructions file at {instructions_path}: {e}"
47-
)
48-
continue
49-
50+
dev_path = Path(__file__).parent.parent / "static" / filename
51+
52+
for file_path in [packaged_path, dev_path]:
53+
if file_path.exists():
54+
return file_path
55+
5056
# If neither path works, provide helpful error message
51-
logger.error(f"Instructions file not found at {packaged_path} or {dev_path}")
57+
logger.error(f"File not found at {packaged_path} or {dev_path}")
5258
raise FileNotFoundError(
53-
f"Required instructions file not found. Searched locations:\n"
59+
f"Required file '{filename}' not found. Searched locations:\n"
5460
f" - {packaged_path} (packaged installation)\n"
5561
f" - {dev_path} (development installation)\n"
56-
f"Please ensure the instructions.md file exists in the static directory."
62+
f"Please ensure the file exists in the static directory."
5763
)
5864

5965

66+
def _load_instructions() -> str:
67+
"""Load instructions from the static instructions file."""
68+
try:
69+
instructions_path = _find_static_file("instructions.md")
70+
return instructions_path.read_text(encoding="utf-8")
71+
except Exception as e:
72+
logger.error(f"Error reading instructions file: {e}")
73+
raise
74+
75+
6076
# Initialize FastMCP server
6177
mcp = FastMCP(
6278
"TIM-MCP",
@@ -513,39 +529,28 @@ async def get_content(
513529
@mcp.resource(
514530
uri="whitepaper://terraform-best-practices-on-ibm-cloud",
515531
name="IBM Terraform Whitepaper",
516-
description="IBM Cloud Terraform Best Practices white paper in markdown format.",
532+
description=textwrap.dedent("""
533+
A concise guide to best practices for designing, coding, securing, and operating Terraform Infrastructure as Code solutions on IBM Cloud.
534+
Focuses on modularity, deployable architectures, security governance, and operational workflows.
535+
"""),
517536
mime_type="text/markdown",
518537
tags={"documentation", "best-practices", "terraform"},
519538
meta={
520539
"version": "1.0",
521540
"team": "IBM Cloud",
522541
"update_frequency": "monthly",
523-
"file_size_bytes": (Path(__file__).parent.parent / "static" / "terraform-white-paper.md").stat().st_size
542+
"file_size_bytes": _find_static_file("terraform-white-paper.md").stat().st_size
524543
}
525544
)
526545
async def terraform_whitepaper():
527-
"""IBM Cloud Terraform Best Practices white paper in markdown format."""
528-
whitepaper_path = (
529-
Path(__file__).parent.parent / "static" / "terraform-white-paper.md"
530-
)
546+
whitepaper_path = _find_static_file("terraform-white-paper.md")
531547
return whitepaper_path.read_text(encoding="utf-8")
532548

533549

534550
@mcp.resource(
535551
uri="catalog://terraform-ibm-modules-index",
536552
name="IBM Terraform Modules Index",
537-
description="An compact list of all recommended IBM Terraform modules from the terraform-ibm-modules (TIM) namespace.",
538-
mime_type="application/json",
539-
tags={"index", "modules", "terraform"},
540-
meta={
541-
"version": "1.0",
542-
"team": "IBM Cloud",
543-
"update_frequency": "weekly",
544-
"file_size_bytes": (Path(__file__).parent.parent / "static" / "module_index.json").stat().st_size if (Path(__file__).parent.parent / "static" / "module_index.json").exists() else 0
545-
}
546-
)
547-
async def module_index():
548-
"""
553+
description=textwrap.dedent("""
549554
IBM Terraform Modules Index - A comprehensive list of all IBM Terraform modules.
550555
551556
This resource provides a curated index of IBM Terraform modules from the
@@ -562,10 +567,21 @@ async def module_index():
562567
563568
Use this resource to get an overview of available modules for context enrichment.
564569
For detailed module information (inputs, outputs, etc.), use the get_module_details tool.
565-
"""
566-
module_index_path = Path(__file__).parent.parent / "static" / "module_index.json"
567-
568-
if not module_index_path.exists():
570+
"""),
571+
mime_type="application/json",
572+
tags={"index", "modules", "terraform"},
573+
meta={
574+
"version": "1.0",
575+
"team": "IBM Cloud",
576+
"update_frequency": "weekly",
577+
"file_size_bytes": _find_static_file("module_index.json").stat().st_size
578+
}
579+
)
580+
async def module_index():
581+
try:
582+
module_index_path = _find_static_file("module_index.json")
583+
return module_index_path.read_text(encoding="utf-8")
584+
except FileNotFoundError:
569585
logger.warning("Module index not found, returning empty index")
570586
return json.dumps(
571587
{
@@ -578,8 +594,6 @@ async def module_index():
578594
indent=2,
579595
)
580596

581-
return module_index_path.read_text(encoding="utf-8")
582-
583597

584598
def main(transport_config=None):
585599
"""

0 commit comments

Comments
 (0)