How to Configure Semantic Kernel MongoDB Credentials

Published
Length
4 min read

Luis Felipe Dias

All posts

You set up semantic kernel mongodb credentials by creating a scoped database user role. Then you pass those login details to your vector memory connector at startup. Many teams start by giving the AI app a broad admin account. That shortcut lets a single bad prompt alter or delete entire collections. A safer setup pairs strict database roles with least-privilege access rules. How to set up semantic kernel mongodb credentials with native roles MongoDB uses role-based rules to control what a us

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You set up semantic kernel mongodb credentials by creating a scoped database user role. Then you pass those login details to your vector memory connector at startup. Many teams start by giving the AI app a broad admin account. That shortcut lets a single bad prompt alter or delete entire collections. A safer setup pairs strict database roles with least-privilege access rules.

How to set up semantic kernel mongodb credentials with native roles

MongoDB uses role-based rules to control what a user account can do. A role grants specific actions on defined database resources. Each role applies to the database where an admin defines it. You can scope a role down to the collection level of detail. Note that Atlas users have different built-in roles than self-hosted database users. Still, both types of deployments build roles from the same privilege actions.

Your first step is picking the right role for your AI workload. Some agents only need to search vector embeddings and read stored documents. For those agents, the built-in read role is the safest starting point. The read role grants read access to all non-system collections in its database. It also includes access to the system.js collection. It allows actions like find, listCollections, listIndexes, dbStats, collStats, and changeStream. It does not allow insert, update, remove, or dropCollection actions.

Other agents must save new chat history or write fresh vector embeddings. Those tasks require the built-in readWrite role on the target database. The readWrite role includes every action in the read role. It also adds the ability to change data inside that database. Its actions include insert, update, remove, createCollection, createIndex, dropIndex, and dropCollection. Because dropCollection is included, a custom role is safer for production apps. You can build a custom role that allows find and insert only. That custom role stops the agent from dropping collections or indexes.

Once the role is ready, create a dedicated database user for the app. Assign your custom role or built-in role to that new user account. Store the username and secret key in a secure secret store. Load those values into your app memory when the host process starts. Your memory connector then uses those details to log in and run queries.

Why static database roles are only half the security story

Native database roles solve an important problem at the login step. They decide which user account can log in to a database. They also limit which collections that user account can read or change. If an agent with a read role tries to delete data, MongoDB blocks it.

Still, static roles leave a large gap open while queries run. A role checks who logs in, not what individual queries do in flight. Every agent task shares the same pooled database user account at runtime. When the app runs a query, the database only sees that shared user. You cannot tell which user prompt or agent step triggered a specific find action.

Ephemeral Credentials + MongoDB Authentication & Authorization: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Collection-level roles also cannot filter out sensitive fields inside a single document. If a collection holds customer records, the read role returns the full document. Sensitive fields like emails, phone numbers, card numbers, and national IDs flow back to the model. Unless a tool rewrites matched fields in results, the model receives raw personal data. Also, giving an agent the readWrite role allows remove and dropCollection actions. A static login check cannot pause a dangerous delete command for human approval.

Separating agent tasks by reading and writing needs

You can reduce risk by splitting your AI app into two separate database identities. Create one database user with a read role for search and retrieval tasks. Create a second database user with narrow write rules for ingestion jobs. Your retrieval plugins then use the read-only login details for every user query. Your background indexing worker uses the write login details only when saving new embeddings.

This split shrinks the blast radius if a prompt injection hits your chat interface. The chat agent can only run find actions on the allowed vector collection. It cannot run update, remove, or dropIndex actions because its role lacks them. Teams should also rotate these stored database keys on a regular schedule. Short key lifetimes limit damage if a config file ever leaks.

Check MongoDB commands and documents on the wire

MongoDB roles and SCRAM login decide who opens a session. They do not check single commands or redact sensitive fields inside returned documents. With hoop.dev, you run an open-source proxy, a sidecar for your MongoDB. It reads the MongoDB wire protocol and checks commands and bulk writes as JSON while passing login through untouched.

Before a command runs, hoop.dev guardrails check its effect and refuse banned drops or writes with MongoDB error code 13. On the way back, hoop.dev data masking rewrites sensitive fields in returned documents that match your rules. Every command and verdict is logged. See the open-source code on GitHub.

Frequently asked questions

What is the safest built-in role for a read-only Semantic Kernel agent?

The built-in read role is the safest choice for read-only agent workloads. It allows find and listCollections actions while blocking insert, update, remove, and dropCollection.

Why is the default readWrite role risky for AI agents?

The readWrite role includes destructive actions like remove, dropIndex, and dropCollection. If an agent only needs to add records, create a custom role without drop permissions.

Can MongoDB roles restrict access to a single collection?

Yes, MongoDB roles can grant access down to a single collection. Scoping semantic kernel mongodb credentials to one collection keeps agents away from other app data.

The query runs. The emails stay hidden.