[KongchangAI]
· 3 min read· 1,561 words

Give Your AI Agent a Stock Photo Search Tool in 5 Lines of Code

Give Your AI Agent a Stock Photo Search Tool in 5 Lines of Code

Pexafy gives AI Agents real stock photo semantic search with licensing and attribution, supporting multi-framework integration.

Pexafy is a stock library semantic search engine for AI Agents, covering millions of free photos from Unsplash, Pexels, and Pixabay. It fills the gap left by generative AI in image-sourcing—synthetic images lack realism and verifiable copyright sources, while Pexafy returns real photos with license types and attribution lines. Integration is concise, with native LangChain/LangGraph and Vercel AI SDK support plus a universal MCP server. Results include photo_id, alt_text, url, dimensions, photographer, source, license, and full credit, ready for production. The free tier requires attribution next to published images.

When your AI Agent starts writing blogs, landing pages, or slide decks, sourcing images becomes an unavoidable step. Generative images aren't always the answer—you may need a real photo of a street in Lisbon, a real espresso machine, and a verifiable license.

Pexafy provides a semantic search capability that covers millions of free-to-use photos from stock libraries like Unsplash, Pexels, and Pixabay. You simply describe the image you need in natural language, and the agent returns real photographic works complete with license information and attribution text.

rss source: Give your AI agent a stock photo tool in 5 lines

Why AI Agents Need a Real Stock Photo Tool

Generative AI has clear shortcomings when it comes to sourcing images. Model-generated images may look polished, but when you need to show a "real" office scene or a "real" product usage context, synthetic images tend to lack persuasiveness—and they can't provide a verifiable copyright source.

Pexafy is positioned precisely to fill this gap. It's not an image generation tool, but a semantic search engine: the agent describes its needs in everyday language, and the system returns real photographic works. Every result includes the license type and a ready-to-display attribution line, which is especially important for commercial content scenarios.

The barrier to entry is extremely low. Users only need a free Pexafy API Key (no credit card required) and a key from any model provider. The official example uses Claude Haiku 4.5, but emphasizes that "any provider works."

MCP (Model Context Protocol) is a standard open-sourced by Anthropic in late 2024, designed to unify how large language models communicate with external tools and data sources. Before MCP, every time you integrated a third-party service, developers had to write adapter code for each framework, leaving the tool ecosystem highly fragmented. By defining a common client–server protocol, MCP lets tool providers implement a server once and have it callable by any MCP-compatible client. Pexafy offers an MCP server precisely for this reason: whether users are on Claude Desktop, Cline, or any other MCP-capable host, they can access its stock photo search capability the same way, without adapting to each framework individually. This is also why the article emphasizes that "any framework supporting MCP" can cover a large number of tech stacks at once.

How to Integrate with Mainstream Frameworks

Pexafy offers native integration solutions for different tech stacks, and the official team claims "every piece of code has been run as-is."

LangChain and LangGraph (Python)

After installing langchain-pexafy, langchain, and langchain-anthropic via pip, you can create an agent in just a few lines. The core code calls create_agent and passes in PexafyToolkit().get_tools(), then issues an instruction to the agent, such as "find a hero image for an article about remote work and include the attribution."

The toolkit contains three tools: search by description, find similar images, and fetch a single photo. create_agent runs on top of LangGraph, and these tools are also compatible with ToolNode or bind_tools(). LangChain.js users can use the corresponding npm package.

Vercel AI SDK (TypeScript)

Integration in the TypeScript ecosystem is equally concise. After installing pexafy-ai-sdk, ai, zod, and @ai-sdk/anthropic, simply pass pexafyTools() into generateText. The example uses stopWhen: stepCountIs(5) to limit the number of multi-step calls and prevent the agent from entering an infinite loop.

Any Framework That Supports MCP

For the broader framework ecosystem, Pexafy provides an MCP server (https://mcp.pexafy.com/mcp) with the API Key as a Bearer header. The official team demonstrates integration using Pydantic AI as an example, filtering to keep only the two search tools search_photos and search_photos_by_image—the remaining tools are intended for chat clients that need to display image grids.

The official documentation also lists tested configurations for OpenAI Agents SDK, Claude Agent SDK, Google ADK, CrewAI, LlamaIndex, Mastra, and Strands Agents, covering a remarkably wide range.

LangGraph is an agent orchestration framework introduced by the LangChain team, using a graph structure to describe an agent's execution flow. Unlike earlier approaches that relied on chain-based calls, LangGraph models each step of the agent as a node, with transitions between nodes defined by edges, naturally supporting loops, branches, and conditional jumps—control flows common in real agents. The create_agent mentioned in the article is a high-level wrapper built on top of LangGraph; it automatically sets up the loop of "model thinks—calls tool—observes result—thinks again." Once Pexafy's tools are passed into create_agent, the agent can autonomously decide to call search_photos when an image is needed, then continue with subsequent tasks after getting the result. Understanding this underlying mechanism helps developers use LangGraph's native API directly rather than the high-level wrapper when finer control is required.

The Returned Data Structure

The tools return structured records rather than simple image links. Taking a bicycle photo as an example, each record includes:

  • photo_id: unique identifier
  • alt_text: detailed description, such as "red bicycle parked against a white wall, front wheel facing left and rear wheel facing right"
  • url: image address
  • width / height / orientation: dimensions and orientation
  • photographer: photographer's name
  • source: source stock library, such as Unsplash
  • license: license type
  • credit: complete attribution line, including the license link

The value of this design is that it can be used directly in production. The agent can put the url on the page, write the alt_text into the alt attribute, and place the credit below the image.

It's worth noting that the free tier requires displaying the attribution line next to every published image. This constraint should be written into the agent's instructions as a rule in automated workflows to avoid compliance issues.

The alt_text repeatedly mentioned in the article is the value of the alt attribute in HTML, used to provide a text description when an image cannot be displayed, and also serves as the basis for screen readers to read image content aloud for visually impaired users. It was originally a mandatory requirement for accessibility (a11y), and has now evolved into an important signal for search engines to understand image semantics. The alt_text returned by Pexafy is written by the stock library's photographer or platform staff, and typically includes details such as subject, color, composition, and orientation—for example, "red bicycle parked against a white wall, front wheel facing left and rear wheel facing right." Such descriptions are both accurate and natural, far superior to generic machine-generated phrasing. Developers who write it directly into the alt attribute satisfy accessibility compliance and incidentally complete image-level SEO optimization. It's important to note that alt_text should describe the image itself, not repeat the page title, otherwise it actually diminishes its value.

Practical Recommendations

When integrating a stock photo tool into an agent, a few things are worth noting. First, make the attribution requirement explicit in the system prompt so the agent includes the credit field in every output. Second, make good use of alt_text—it's both an accessibility necessity and an accurate description of image semantics, directly usable for SEO optimization. Third, for scenarios requiring similar image recommendations, search_photos_by_image is more efficient than text search.

From a broader perspective, tools like Pexafy reflect a trend in AI Agent capability expansion: rather than having the model generate everything, it's better to equip it with "hands" that can call specialized services. The traceable licensing of real photos is an advantage that synthetic images cannot easily replace in the short term. For content teams, this means the agent's drafts can go directly into the publishing workflow without being artificially interrupted at the image-sourcing step.

Additional Background

In actual engineering practice, giving an agent a stock photo tool also requires considering failure and fallback paths. Semantic search doesn't always hit the ideal result, especially when the description is too abstract (such as "a tech-feeling image"), where it may return photos that deviate from expectations. A prudent approach is to specify in the system prompt that if the first search result doesn't meet requirements, the agent should rephrase the query and try again, or call search_photos_by_image to find similar material using an existing image as a reference. Additionally, the free tier's attribution requirement means a validation step is needed in the publishing workflow—confirming that every image's credit field is rendered on the page. For scenarios involving batch content generation, it's recommended to add an automated check before final output, scanning whether all images on the page have accompanying attribution, thereby preventing copyright risks at the source. These details may seem minor, but they determine whether the agent can truly run the image-sourcing step unattended.

One-line Summary

Pexafy provides real stock library semantic search for AI Agents, with licensing and attribution info, supporting multi-framework integration.

Paragraph Summary

Pexafy is a stock library semantic search engine for AI Agents, covering millions of free photos from Unsplash, Pexels, Pixabay, and other platforms. It fills the gap left by generative AI in image-sourcing scenarios—synthetic images lack realism and verifiable copyright sources, whereas Pexafy returns real photographic works with license types and attribution lines. Integration is concise, offering native integrations for LangChain/LangGraph and Vercel AI SDK, plus a universal MCP server; the official team claims all example code runs as-is. Returned results include photo_id, alt_text, url, dimensions, photographer, source, license, and a complete attribution line, ready for production. The free tier requires displaying attribution next to published images, and this compliance constraint must be made explicit in the agent's instructions.

#Pexafy#AI Agent stock photo tool#LangChain stock photo search#Vercel AI SDK image search#MCP server
Share:

Related articles