AI

Give Your AI Agent an IP Lookup Tool With Function Calling

C# Corner Abdullah Afzal
AI

A C# walkthrough of one IP lookup tool shared by OpenAI's Responses API and Anthropic's Messages API, covering schema design, input validation, response trimming and the API key that .NET 8 logging writes into your logs.

Key points

  • The model only returns a tool name and JSON arguments; your code makes the call and appends the result.
  • One shared `ParametersJson` schema with `additionalProperties: false` and strict mode keeps arguments predictable for both providers.
  • `IPAddress.TryParse` accepts "8.8", "134744072" and "0x08080808", so the code compares against the canonical dotted form.
  • Reserved ranges are checked locally with `IPNetwork.Contains` on .NET 8's `System.Net.IPNetwork` before any network call.
  • Trimming the vendor response from 475 to 66 tokens cuts what is re-sent with every later request.
  • Microsoft.Extensions.Http 8.0.1 logs the full request URI, query-string API key included; 9.0.0 and later redact it.

Why it matters

Anyone wiring tools into an agent with raw OpenAI or Anthropic SDK calls gets a working reference here, and the parts worth stealing are not the loop: input validation with `IPNetwork`, a fixed error contract, and a response shape you control. The urgent finding is the logging one — if you call `AddHttpClient` on Microsoft.Extensions.Http 8.x and pass secrets in query strings, the key is already sitting in your Information logs. Check that before starting a new agent, or move to a newer version.

Microsoft.Extensions.AI already ships `AIFunction` wrappers, so treat this class as the layer beneath those abstractions rather than a replacement. Before writing provider-specific adapters, see how much of your case the existing abstraction covers; keeping control of schema, validation and response shape is what actually decides the cost of a tool like this.

Real-world use

  • An agent that answers "where is this IP?" while triaging support tickets or reading access logs.
  • A shared tool layer where VPN and threat-intel tools are registered only for admin sessions.
  • Switching geolocation vendors such as IPinfo, MaxMind GeoIP2, ip-api or IPLocate without changing the shape the model reads.
  • Agents calling a metered API where a failed call must return a fixed error instead of triggering silent retries.

This summary was generated by the agent from the original post on C# Corner. The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message