Integrating OpenCode with MongoDB data via CData CLI

Justin Floyd
Justin Floyd
Product Business Analyst
CData CLI gives AI coding agents direct, command-line-native access to CData Drivers across hundreds of data sources, allowing agents to manage licenses, configure connections, run SQL queries, and explore schema metadata, all without leaving the terminal.

OpenCode is an open source AI coding agent from Anomaly that runs in your terminal, desktop, or IDE. It connects to over 75 LLM providers, including Claude, GPT, Gemini, and local models, and stores none of your code or context data. It runs multiple agent sessions in parallel on the same project, each with its own context. Its support for integrations, AGENTS.md configuration files, and a TypeScript/JavaScript plugin system makes it well-suited for tool-driven data workflows through CData CLI. By describing your data goals in plain language, OpenCode handles driver configuration, license activation, connection creation, and query execution from a single prompt.

This article walks through how to connect MongoDB data to OpenCode through CData CLI.

Prerequisites

  1. OpenCode installed (either Terminal CLI or Desktop): OpenCode | Download
  2. CData CLI installed
  3. Access to MongoDB

About MongoDB Data Integration

Accessing and integrating live data from MongoDB has never been easier with CData. Customers rely on CData connectivity to:

MongoDB's flexibility means that it can be used as a transactional, operational, or analytical database. That means CData customers use our solutions to integrate their business data with MongoDB or integrate their MongoDB data with their data warehouse (or both). Customers also leverage our live connectivity options to analyze and report on MongoDB directly from their preferred tools, like Power BI and Tableau.

For more details on MongoDB use case and how CData enhances your MongoDB experience, check out our blog post: The Top 10 Real-World MongoDB Use Cases You Should Know in 2024.


Getting Started


Step 1: Download the skill (one-time setup)

Always use CData CLI with the official Skill.

  1. The official CData CLI Skill on GitHub can be downloaded using npx skills through the terminal:

    npx skills add CDataSoftware/cli-skills

  2. Follow the prompts in the terminal to install for OpenCode. Installing the CData CLI skill for OpenCode

Step 2: Set up the project directory

Create a project directory to contain all project files. Below are examples for using OpenCode in either the Terminal or Desktop app.

OpenCode in Terminal CLI

Navigate to your desired directory in the terminal and start a session with the opencode command.

Starting an OpenCode CLI session Starting an OpenCode CLI session (2)

OpenCode Desktop App

Open the desktop app and select your project folder as the working directory.

Selecting the working directory in OpenCode Desktop

Step 3: Establish the driver and connection

Describe what you want to accomplish with the CLI and MongoDB data.

I would like to build a command line app that connects to MongoDB and checks for updates from restaurants. Make sure to include data from important columns like borough and cuisine.

This prompt automatically loads the Skill and kicks off the following process. You can also manually prompt the agent for each of the following steps.

  1. Driver setup: OpenCode checks for an existing CData MongoDB driver, or searches and downloads a new one:
    • cdatacli drivers list
    • cdatacli drivers search --driver "MongoDB"
    • cdatacli drivers download --artifact-id <artifact-id>
  2. Activation: Activate the MongoDB driver with a single command for a trial or full license:
    • cdatacli drivers activate "MongoDB" --name "<name>" --email "<email>" --trial
    • cdatacli drivers activate "MongoDB" --name "<name>" --email "<email>" --key "<product-key>"
  3. Establish the connection: Check for existing MongoDB connections or create a new one:
    • cdatacli connection list
    • cdatacli connection create --driver MongoDB --name my_mongodb_connection --connectionstring "Prop1=value1;Prop2=value2;..."
  4. Create a MongoDB skill (if applicable): CData provides driver instructions for popular sources that can be used to create a source-specific skill to guide the agent through best practices for the driver.
    • Run the following command to generate a skill file and save the output to your skills directory. You can choose to save the skill either at the project level or globally. (Note: If "No instructions available for MongoDB" error is returned, no driver instructions exist and you can continue to use main driver skill)
      cdatacli drivers skill "MongoDB" > ~/skills/cdata-mongodb/SKILL.md

Step 4: Query MongoDB data

With the CData driver configured, your agent can execute queries and write code against live MongoDB data:

  1. cdatacli query sql --connection my_mongodb_connection --sql "SELECT * FROM restaurants"

Query MongoDB data directly from your terminal with CData CLI

OpenCode and CData CLI together give your AI coding agent a direct path to live MongoDB data without custom middleware, scheduled syncs, or manual setup at each step. Describe your goal, and the agent handles driver configuration, connection setup, and query execution.

Download the free CData CLI and start a free 30-day trial of the CData JDBC Driver for MongoDB today.

Ready to get started?

Download a free trial of the MongoDB Driver to get started:

 Download Now

Learn more:

MongoDB Icon MongoDB JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with MongoDB.