Analyze a workload and open a ServiceNow ticket
A Python agent built with LangChain deepagents that answers your prompt about one instance through AI Connector and opens a ServiceNow incident when it finds a problem.
This developer guide shows an unattended job built on the Datasentinel MCP server and Skill. You give it a prompt, for example "Analyze yesterday's workload on pg-crm". The agent answers it on one PostgreSQL instance and looks for problems such as lock contention, I/O or CPU pressure and regressed queries. The script then pushes the result as an incident into your ServiceNow account.
The script extends Build a Python agent with deepagents. Read that page first: the MCP connection, the Skill and the token handling are the same.
The script does two things:
Analyze. A deep agent gets every MCP tool and the Datasentinel Skill. It reads the instance and the time window from your prompt, follows the matching Skill procedure and chooses its own tool calls. It must answer in a fixed structure (
response_format): the instance and window it analyzed, a verdict, findings with a category, a severity, evidence and a recommendation, and a Markdown report.Notify. If the overall severity reaches the threshold you set, the script creates an incident through the ServiceNow Table API. The incident carries a correlation id built from the instance and the window the agent analyzed. Running the same prompt twice for the same day adds a work note to the open incident instead of opening a duplicate.
Use it as a starting point. The ServiceNow part is one short function, easy to swap for Jira, PagerDuty or an email.
Prerequisites
Python 3.10 or later.
Your Datasentinel MCP server is reachable over HTTPS at
https://<public-host>/mcp. See MCP Server Setup if it is not yet running.The Datasentinel Skill archive,
datasentinel-skill.zip, downloadable from https://app.datasentinel.io/ds-api/download/datasentinel-skill.zip.An API key for the model provider you want the agent to use. This example uses Anthropic's Claude.
A Datasentinel user with the AI Connector add-on, and its Grafana login and password. The script mints the user token itself and keeps it valid, as in Refresh the user token automatically.
A ServiceNow integration user with the
itilandrest_api_explorerroles, allowed to create and update records in theincidenttable. If your ServiceNow instance restricts inbound API calls by IP address, allow the machine that runs the script.
Set up the project
Install deepagents, the MCP adapter that turns MCP tools into agent tools, your model provider's package, and httpx:
Export your model provider's API key:
Extract the Skill next to your script so the agent can load it. This produces a datasentinel/ folder containing SKILL.md:
Export the Datasentinel and ServiceNow settings. Keep every credential in environment variables or your scheduler's secret store, never in the script.
Write the agent
The script takes your prompt from the command line. It connects to the Datasentinel MCP server over HTTPS and loads its tools. It builds a deep agent around those tools plus the Datasentinel Skill, and sends it the prompt, preceded by the current local time so "yesterday" resolves to real dates. The agent must answer with the Analysis Pydantic model (response_format). The script reads it from result["structured_response"], so it never parses free text. It then builds the incident and sends it to ServiceNow.
Save this as servicenow_workload_analysis.py, next to the skills/ folder:
Notes:
The agent runs non-interactively. The system prompt forbids questions and tells it to follow the Skill procedure that matches the prompt. When the prompt names no instance, the agent analyzes the most loaded one. When it names no window, it analyzes yesterday. The agent chooses its own tool calls.
The system prompt cancels the Skill's closing menu explicitly. The Skill normally ends every result with a numbered "What would you like to do next?" menu, which would land in the incident's work notes. The Skill exempts a non-interactive run that the host declares, and the last paragraph of
SYSTEM_PROMPTis that declaration. The follow-ups the menu would have offered are written into the report as sentences instead.skills=["./skills/"]points at a parent directory, as in Build a Python agent with deepagents. The script warns on stderr whenskills/datasentinel/SKILL.mdis missing.The user token travels as the
Authorization: Bearerheader of the MCP connection. The model never sees it. See How authentication works.AGENT_MODELaccepts any model LangChain supports, for example a local Gemma 4 through Ollama (ollama:gemma4:e4b, withpip install langchain-ollama). The model must support tool calling.To send the result somewhere else, replace
upsert_incident. The analysis is already structured.
Run it
Start with a dry run. The analysis runs end to end and the script prints the incident it would create. Nothing is sent to ServiceNow.
The script first prints a header on stderr, like the deepagents agent. It tells you which MCP server, tools, model and Skill the run uses. The last line gives the instance and window the agent analyzed and the verdict:
Minted a new Datasentinel user-token in ~/.datasentinel_token appears when the cached token was missing or expired. A WARNING: no skills loaded from ./skills/ line means the Skill was not extracted next to the script: the agent would then run without it. Only the incident (or, after a real run, its number) goes to stdout.
The prompt is the last argument. Quote it, or the shell splits it into words (the script joins them back). Name the instance as Datasentinel shows it. Times are in the platform's local time. Any request the Skill covers works, for example:
Other options:
--min-severity warning (default) or critical
the lowest overall severity that opens a ticket. Below it the script prints "nothing to report" and exits 0
--out <file>
write the full analysis as JSON, for archiving or a second channel
--dry-run
print the incident, send nothing to ServiceNow
Once the dry run looks right, schedule it:
What the incident looks like
Short description:
[Datasentinel] Degraded workload on pg-crm@srv1:5432 — 2026-09-16Description: the plain-language summary, then one numbered entry per finding with its severity, category, evidence and recommendation.
Work notes: the Markdown report in the Skill's shape.
Impact / urgency: 2 / 2 for a critical severity, 3 / 3 for a warning.
Category:
Database. Assignment group, caller: from the environment variables above.Correlation id:
datasentinel:<instance>:<window>. Correlation display:Datasentinel.
A second run for the same instance and window, whatever the wording of the prompt, finds the open incident by its correlation id and appends a work note. Closing the incident in ServiceNow is a human decision. A later run for the same window opens a new one only if none is active.
The script exits 0 when it is done, including "nothing to report", and 2 when the prompt is missing. Any other error exits 1 with its traceback, so your scheduler's monitoring sees it.
Last updated