All articles

Setting Tool Boundaries for a Location Assistant

Keeping ownership checks in verified server-side context, attaching freshness timestamps to location results, and returning structured missing states to support honest replies.

  • AI engineering
  • Tool Calling
  • Device messaging

Do Not Enforce Access Control in a System Prompt

On the pet tracker project, I connected device location queries to a conversational assistant. Exposing a backend lookup as a callable tool is mechanically straightforward, but two design boundaries require strict discipline: server-enforced authorization and clear representation of stale or absent readings. The following is an illustrative design built from that background rather than a claim that every detail shown here was shipped verbatim.

Picture two hypothetical situations. In the first, an authenticated user asks, “Where is device collar-902 right now?”, when collar-902 belongs to someone else—or a prompt-injection snippet nudges the model to substitute another identifier. In the second, the user asks where their own tracker went over the last hour, but the collar has been powered off all afternoon.

If the tool exposes userId as an LLM-supplied argument, or relies on prompt instructions telling the model not to read other accounts’ devices, access control becomes probabilistic rather than enforced—the exact excessive-agency risk highlighted in the OWASP Top 10 for LLM Applications. Likewise, if an empty time window returns a bare [], a helpful-sounding model may extrapolate and tell the user that their pet stayed still instead of explaining that no readings arrived.

Put Identity and Ownership in Verified Server Context

A cleaner boundary separates untrusted model arguments from trusted execution context. The model supplies only intent parameters such as a device reference or query window, while authenticated caller identity enters outside the tool’s JSON schema. In Spring AI’s tool-calling model, ToolContext passes request-scoped context into tool methods without exposing those fields to the model. Crucially, ToolContext is only trustworthy when populated by validated application authentication and authorization middleware, never copied from user-controlled fields or model-generated arguments.

The pseudocode below illustrates enforcing ownership at the tool entry point:

// Pseudocode: enforcing caller ownership through verified server-side context
function getLatestLocation(deviceRef, toolContext):
  callerId = toolContext.requireVerifiedUserId()
  device = deviceRepo.findByRefAndOwner(deviceRef, callerId)

  if device == null:
    return { status: "NOT_FOUND_OR_UNAUTHORIZED" }

  fix = locationRepo.findLatestFix(device.id)
  if fix == null:
    return { status: "NO_LOCATION_RECORDED", deviceName: device.name }

  ageSeconds = nowUtc() - fix.recordedAt
  status = ageSeconds > STALE_THRESHOLD_SECONDS ? "STALE" : "FRESH"
  return buildLocationResponse(status, device.name, fix, ageSeconds)

Returning a uniform NOT_FOUND_OR_UNAUTHORIZED status blocks cross-account reads without leaking whether an arbitrary device identifier exists in the database.

Carry Timestamps and Explicit Missing States

A location answer without a timestamp is incomplete. If the latest fix in the database is three hours old, stating “the collar is at the north gate” misleads the user. Returning an RFC 3339 observedAt timestamp, an explicit ageSeconds field, and a freshness status helps distinguish a live reading from a last-known position:

{
  "status": "STALE",
  "deviceName": "Milo",
  "observedAt": "2026-12-03T05:10:00Z",
  "ageSeconds": 10800,
  "coordinates": { "lat": 31.2304, "lon": 121.4737 },
  "note": "No newer fix received since observedAt."
}

For windowed track queries with zero matching points, returning a structured NO_DATA_IN_WINDOW payload—including the queried window bounds and optionally the last known fix time—gives the assistant concrete facts to cite. Still, structured responses support faithful answers rather than guaranteeing the model will never hallucinate or drop a caveat, which is why response-level evaluation checks remain necessary.

Tradeoffs and Verification

Structured status envelopes make tool payloads slightly larger and require keeping prompt instructions aligned with the status enum. You also typically need a companion read-only tool scoped by the same verified ToolContext to list the caller’s own devices, so the model resolves names to owned IDs instead of guessing identifiers.

Before relying on the integration, three test categories are worth automating:

  • Cross-account and prompt-injection checks: ask directly for another account’s device ID or embed override instructions, confirming the tool denies access uniformly without revealing existence.
  • Stale and empty-window response evaluations: feed STALE and NO_DATA_IN_WINDOW tool outputs into evaluation tests and verify the reply cites observedAt and states clearly when recent data is missing.
  • Time-zone boundary checks: test queries around midnight (“today”, “the last hour”) to verify RFC 3339 offsets are resolved consistently on the server.

References