Oci mcp智能查询
Oci mcp智能查询 是一个使用人工智能的助手 模型上下文协议(MCP) 和 生成型人工智能(Ollama/OpenAI兼容模型) 到 使用自然语言编排Oracle云基础架构(OCI)资源.
你可以:
- 用简单的英语问:
- “从以下位置获取所有实例 test 隔间” - “创建计算实例 SMARTVMTEST 在 root/test 使用任何子网” - “终止 AUTOTEST 实例在 test 隔间”
- 让 规划师LLM 制定执行计划。
- 拥有 MCP客户端 使用 MCP服务器,它通过官方Python SDK与OCI通信。
- 使用 流线型UI 它看起来类似于现代的人工智能聊天客户端。
______________________________________________________________________
1.特点
- 🔹 自然语言→ OCI行动 通过Ollama/LLM规划师
- 🔹 MCP服务器 公开OCI工具:
- get_compartment_ocid - list_instances - get_instance_by_name - get_available_subnets - get_subnet_by_name - get_latest_image_by_prefix - get_images_by_prefix - create_compute_instance - delete_instance
- 🔹 智能规划 -规划者决定调用哪些工具以及如何将它们链接起来
- 🔹 流线型UI 具有类似控制台的输出
- 🔹 设计为 可扩展的 更多OCI服务,甚至未来的AWS
______________________________________________________________________
2.仓库布局
OCI-MCP-SmartQueryAI/
├── common/
│ └── utils.py
├── config/
│ └── settings.yaml # OCI & app configuration (edit this)
├── docs/
│ └── architecture.md
├── mcp_server/
│ ├── mcp_oci_server.py # FastAPI + MCP server for OCI tools
│ ├── oci_helper.py # OCI SDK helper methods
│ └── tools/ # MCP tool registration
├── mcp_client/
│ ├── mcp_client_helper.py # MCP HTTP client wrapper
│ ├── streamlit_app.py # (optional) Streamlit entrypoint
│ └── ollama/
│ ├── smart_ollama_mcp_client.py # CLI AI planner client
│ └── planner_prompt.txt # System prompt for planner LLM
├── scripts/
│ └── test_mcp_client.py # Simple direct MCP client test
├── voice-assistant/
│ └── speech_to_text.py # Future voice integration
├── control_services.sh # Helper to start/stop MCP server + Streamlit UI
├── requirements.txt
└── README.md______________________________________________________________________
3.先决条件
3.1.操作系统/运行时
- Linux(在类似Oracle Linux/RELL的发行版上测试)
- python 3.10+ (用3.12测试)
bash/POSIX外壳(用于control_services.sh)
3.2.OCI设置
- OCI配置文件
创建/验证 ~/.oci/config 使用配置文件(默认值: DEFAULT):
[DEFAULT]
user=ocid1.user.oc1..aaaaaaaa...
fingerprint=aa:bb:cc:dd:...
key_file=/home//.oci/oci_api_key.pem
tenancy=ocid1.tenancy.oc1..aaaaaaaa...
region=us-phoenix-1- 政策/IAM
与您的配置关联的用户(或组)必须具有以下权限:
Allow group to inspect compartments in tenancy
Allow group to read virtual-network-family in tenancy
Allow group to inspect images in tenancy
Allow group to manage instance-family in compartment 调整范围(tenancy 与特定隔间相比)。
______________________________________________________________________
3.3。奥拉马/LLM
安装并运行 奥拉玛 在可从MCP客户端访问的机器上:
# On the Ollama host
curl -fsSL https://ollama.com/install.sh | sh # or use the official installer
ollama serve # typically listens on :11434
# Pull a suitable model (e.g., llama3.2)
ollama pull llama3.2然后在环境中设置(在运行客户端/UI的计算机上):
export OLLAMA_URL="http://
:11434/api/chat"
export OLLAMA_MODEL="llama3.2"您可以根据需要交换其他支持聊天的模型,只要它们支持OpenAI风格 /api/chat 通过Ollama接口。______________________________________________________________________
4.克隆和安装
# 1) Clone the repository
git clone https://github.com/bhaskaro/OCI-MCP-SmartQueryAI.git
cd OCI-MCP-SmartQueryAI
# 2) Create & activate virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# 3) Install dependencies
pip install --upgrade pip
pip install -r requirements.txt
# 4) Ensure Python can see this repo as a package
export PYTHONPATH="$(pwd)"______________________________________________________________________
5.配置
5.1. config/settings.yaml
更新配置文件以匹配您的环境。典型的布局可能包括:
oci:
profile: DEFAULT # profile from ~/.oci/config
region: us-phoenix-1 # OCI region
default_compartment: test # logical name used by your helper / planner
server:
host: 0.0.0.0
port: 8000
streamlit:
host: 0.0.0.0
port: 8501调整按键/结构以匹配当前 settings.yaml 如果回购发生了变化。这里是 更新了5.2环境变量部分,重写得很干净,以匹配你的新 .env-基于配置模型。 你可以 将此内容直接复制并粘贴到README.md中.
______________________________________________________________________
5.2.环境变量
应用程序现在从以下位置加载配置 .env 文件 使用 python-dotenv. 这使敏感值远离源代码,并简化了部署。
步骤1——创建 .env 文件
从项目根:
cd OCI-MCP-SmartQueryAI
cat > .env :8501/`
______________________________________________________________________
## 8.通过CLI(智能Ollama MCP客户端)进行测试
AI规划器客户端位于 `mcp_client/ollama/smart_ollama_mcp_client.py`.
### 8.1.基本模式
从回购根目录(venv已激活,PYTHONPATH已设置):
python -m mcp_client.ollama.smart_ollama_mcp_client "your natural language query here"
### 8.2.示例
#### 8.2.1.列出隔间中的实例
python -m mcp_client.ollama.smart_ollama_mcp_client \ "get list of instances from test compartment"
预期规划师计划(简化):
{ "steps": [ { "tool": "get_compartment_ocid", "args": { "compartment_name": "test" }, "save_as": "compartment_ocid" }, { "tool": "list_instances", "args": { "compartment_ocid": "$compartment_ocid" }, "save_as": "instances" } ] }
客户将打印:
- 生成的计划
- 每次MCP工具调用
- 最终变量,包括 `instances` 包含实例详细信息。
______________________________________________________________________
#### 8.2.2.创建计算实例
查询示例:
python -m mcp_client.ollama.smart_ollama_mcp_client \ "create compute instance with the name SMARTVMTEST in compartment root/test. \ Take whatever subnet available in the same compartment."
典型方案:
1. `get_compartment_ocid` → 解决 `root/test`
1. `get_available_subnets` → 在该隔间中选择一个子网
1. `get_latest_image_by_prefix` → 例如,最新 `oracle-linux` 图像
1. `create_compute_instance` → 已解析OCID和默认值:
- 形状: `VM.Standard.E2.1.Micro` (或按配置)
- 可选的 `cpu_mem_shape` 用于弯曲
- 子网OCID
- 图像OCID
______________________________________________________________________
#### 8.2.3.获取单实例详细信息
python -m mcp_client.ollama.smart_ollama_mcp_client \ "get AUTOTEST instance details from test compartment"
规划师应:
1. 解析隔间OCID
1. 呼叫 `get_instance_by_name` (不是 `list_instances`)
______________________________________________________________________
#### 8.2.4.终止实例
python -m mcp_client.ollama.smart_ollama_mcp_client \ "terminate AUTOTEST instance in test compartment"
规划师应:
1. 解析隔间OCID
1. `get_instance_by_name`
1. `delete_instance` (使用 `$instance.id`)
底层辅助方法将:
- 呼叫 `terminate_instance`
- 民意调查 `TERMINATED` 状态达到配置的超时时间
- 返回结构化结果(例如。, `{ "instance_ocid": "...", "status": "TERMINATED" }`)
______________________________________________________________________
#### 8.2.5.按前缀获取最新图像
python -m mcp_client.ollama.smart_ollama_mcp_client \ "get latest image with prefix oracle-linux from compartment root"
计划员:
1. `get_compartment_ocid` 为了 `root`
1. `get_latest_image_by_prefix` 随着 `"oracle-linux"`
帮手:
- 用途 `list_images` 随着 `sort_by=TIMECREATED`, `sort_order=DESC`
- 按不区分大小写的前缀筛选
- 返回最新匹配的图像(id、name、time_created、lifecycle_state)
______________________________________________________________________
#### 8.2.6.按前缀获取所有图像
python -m mcp_client.ollama.smart_ollama_mcp_client \ "get all images with prefix oracle-linux from compartment root"
计划员:
1. `get_compartment_ocid`
1. `get_images_by_prefix`
助手使用 `list_call_get_all_results` 拿来 **所有页面** 并返回所有匹配图像的列表。
______________________________________________________________________
## 9.使用Streamlit UI
曾经 `control_services.sh` (或手动命令)已启动UI:
1. 打开浏览器:
`http://:8501/`
1. 您将看到:
- A. **头球** (例如,“OCI-MCP SmartQueryAI控制台”)
- A. **红色水平线** 标题下
- A. **文本框** 输入您的自然语言查询
- A. **提交** 按钮
- 控制台风格区域,打印:
- 生成的计划
- 每次MCP工具调用
- 最终变量/结果
- A. **页脚** 另一条红线和一条联系线(为保护隐私,电子邮件被屏蔽)
示例查询(与CLI相同):
- `get list of instances from test compartment`
- `create compute instance with the name SMARTVMTEST in compartment root/test`
- `terminate AUTOTEST instance in test compartment`
- `get all images with prefix oracle-linux from compartment root`
______________________________________________________________________
## 10.可扩展性
OCI MCP SmartQueryAI旨在 **开放的**:
1. **添加新的辅助方法** 在 `mcp_server/oci_helper.py`
(例如,卷、负载平衡器、对象存储、自治数据库)。
1. **将它们注册为MCP工具** 在 `mcp_server/mcp_oci_server.py`.
1. **把它们交给计划者** 通过更新 `planner_prompt.txt` 与:
- 工具名称
- 参数
- 行为规则/何时使用
1. 可选择将计划器提示调整为:
- 亚马逊云服务
- 多云场景
- 高阶工作流(例如,“克隆此实例”、“快照然后终止”)
这使得该框架成为 **全AI驱动的云编排**.
______________________________________________________________________
## 11.安全注意事项
- 这个工具可以 **创建和销毁真实的OCI资源**.
- 始终进行测试 **非生产区** 第一。
- 考虑添加:
- “干运行”模式
- 明确确认破坏性操作(`delete_instance`等等)
- 日志/审计挂钩
______________________________________________________________________