LangChain toolkit for Universal Commerce Protocol (UCP)
Project description
LangChain UCP
LangChain toolkit for Universal Commerce Protocol (UCP) with A2UI support.
Basic Agent Demo
Overview
langchain-ucp provides LangChain tools and toolkit for building AI agents that can interact with UCP-compliant merchants. It includes A2UI (Agent-to-User Interface) support for rendering rich, interactive UIs.
Features
- Full UCP Support: Complete checkout flow - search, cart, shipping, payment
- A2UI Integration: Render rich UIs with product cards, checkout forms, order confirmations
- LangChain Native: Works seamlessly with LangChain and LangGraph agents
- Type-Safe: Full Pydantic model support
Installation
# Basic installation
pip install langchain-ucp
# With A2UI support (recommended)
pip install langchain-ucp[a2ui]
# All features
pip install langchain-ucp[all]
Quick Start
Basic Usage (Text Only)
from langchain_ucp import UCPToolkit, Product
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
# Define your product catalog
products = [
Product(id="roses", title="Red Roses"),
Product(id="tulips", title="Spring Tulips"),
Product(id="orchid", title="White Orchid"),
]
# Create toolkit with product catalog
toolkit = UCPToolkit(
merchant_url="http://localhost:8000",
products=products,
)
# Create agent
llm = ChatOpenAI(model="gpt-4o")
agent = create_react_agent(llm, toolkit.get_tools())
# Run agent
result = await agent.ainvoke({
"messages": [{"role": "user", "content": "I want to buy some red roses"}]
})
With A2UI (Rich UI Rendering)
A2UI is not a tool - it's an output format standardization. The LLM generates A2UI JSON directly in its response using a delimiter pattern (---a2ui_JSON---).
from langchain_ucp import UCPToolkit, Product
from langchain_ucp.a2ui import (
get_a2ui_system_prompt,
parse_a2ui_response,
A2UI_DELIMITER,
)
from langchain_openai import ChatOpenAI
from langchain_core.messages import SystemMessage, HumanMessage
# Create toolkit
toolkit = UCPToolkit(
merchant_url="http://localhost:8000",
products=products,
)
# Build system prompt with A2UI instructions
base_prompt = "You are a helpful shopping assistant for a flower shop."
system_prompt = base_prompt + get_a2ui_system_prompt(
include_schema=True,
include_commerce_examples=True,
)
# Create LLM and get response
llm = ChatOpenAI(model="gpt-4o")
response = await llm.ainvoke([
SystemMessage(content=system_prompt),
HumanMessage(content="Show me your products"),
])
# Parse response to extract text and A2UI JSON
text_content, a2ui_messages = parse_a2ui_response(response.content)
# text_content: "Here are our available products!"
# a2ui_messages: [{beginRendering...}, {surfaceUpdate...}, {dataModelUpdate...}]
A2UI Architecture
UCP with A2UI - Rich UI Shopping Experience
A2UI works through a delimiter-based approach:
- System Prompt: Use
get_a2ui_system_prompt()to add A2UI instructions - LLM Response: LLM generates text + delimiter + A2UI JSON
- Parse Response: Use
parse_a2ui_response()to split text and A2UI
LLM Response Format:
┌─────────────────────────────────────────┐
│ Here are the products you requested! │ ← Text content
│ │
│ ---a2ui_JSON--- │ ← Delimiter
│ [{"beginRendering": ...}, │
│ {"surfaceUpdate": ...}, │ ← A2UI JSON array
│ {"dataModelUpdate": ...}] │
└─────────────────────────────────────────┘
A2UI Message Types
| Message Type | Description |
|---|---|
beginRendering |
Initialize a UI surface with root component and styles |
surfaceUpdate |
Define the component tree for a surface |
dataModelUpdate |
Update data values that components reference |
deleteSurface |
Remove a surface |
Available A2UI Templates
| Template | Description |
|---|---|
ProductCardTemplate |
Single product card with image, name, price, add to cart |
ProductListTemplate |
Scrollable list of products |
CheckoutTemplate |
Checkout form with shipping address |
OrderConfirmationTemplate |
Order confirmation with success status |
A2UI Helper Functions
from langchain_ucp.a2ui import (
# Prompt helpers
get_a2ui_system_prompt,
parse_a2ui_response,
validate_a2ui_json,
get_a2ui_schema,
A2UI_DELIMITER,
# Template functions
create_product_card,
create_product_list,
create_checkout_ui,
create_order_confirmation,
)
# Create a product card
card = create_product_card(
product_id="roses",
name="Red Roses",
price="$29.99",
image_url="https://example.com/roses.jpg",
description="Beautiful fresh red roses",
)
# Create a product list
products_list = create_product_list(
title="Featured Products",
products=[
{"id": "roses", "name": "Red Roses", "price": "$29.99", "imageUrl": "..."},
{"id": "tulips", "name": "Tulips", "price": "$19.99", "imageUrl": "..."},
],
)
# Create checkout UI
checkout = create_checkout_ui(
checkout_id="chk_123",
items=[{"title": "Red Roses", "quantity": 2, "total": "$59.98"}],
total="$64.98",
)
# Create order confirmation
confirmation = create_order_confirmation(
order_id="ord_456",
items_summary="2x Red Roses",
total="$64.98",
shipping_address="123 Main St, City, ST 12345",
)
Standard A2UI Components
The standard component catalog includes:
- Layout: Row, Column, List, Card, Tabs, Divider, Modal
- Content: Text, Image, Icon, Video, AudioPlayer
- Input: Button, CheckBox, TextField, DateTimeInput, MultipleChoice, Slider
Product Catalog
The Product model is for agent-side discovery.
| Field | Type | Required | Description |
|---|---|---|---|
id |
str | Yes | Unique product identifier (must match merchant's product ID) |
title |
str | Yes | Product display name (used for search) |
Note: When UCP product discovery is implemented, the full product catalog (pricing, categories, descriptions, images) will be fetched directly from the merchant.
Available Tools
| Tool | Description |
|---|---|
search_shopping_catalog |
Search the product catalog |
add_to_checkout |
Add products to cart |
remove_from_checkout |
Remove products from cart |
update_checkout |
Update product quantities |
get_checkout |
View current cart |
update_customer_details |
Add buyer info and address |
start_payment |
Prepare checkout for payment |
complete_checkout |
Complete purchase |
cancel_checkout |
Cancel checkout |
get_order |
Get order details |
Configuration
toolkit = UCPToolkit(
merchant_url="http://localhost:8000", # UCP merchant URL
products=products, # Product catalog
agent_name="my-agent", # Agent name for headers
verbose=False, # Enable debug logging
)
Examples
See the examples directory:
basic_agent.py- Simple agent that adds items to cartinteractive_chat.py- Interactive chat with the shopping agenta2ui_agent.py- Agent with A2UI rich UI rendering
License
Apache License 2.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langchain_ucp-0.2.0.tar.gz.
File metadata
- Download URL: langchain_ucp-0.2.0.tar.gz
- Upload date:
- Size: 30.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc84478e9211427339292b118d9f1669f1bc53ae277503c58e386099e02e57f1
|
|
| MD5 |
3d074b23bfe917a0ad0fa218ff04f836
|
|
| BLAKE2b-256 |
98d66f2acd46f4b96c210b7a3fb75206e46ddc4290bb2c408b1b5d2fe0ee1b18
|
Provenance
The following attestation bundles were made for langchain_ucp-0.2.0.tar.gz:
Publisher:
publish.yml on muzaffersenkal/langchain-ucp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
langchain_ucp-0.2.0.tar.gz -
Subject digest:
fc84478e9211427339292b118d9f1669f1bc53ae277503c58e386099e02e57f1 - Sigstore transparency entry: 912970937
- Sigstore integration time:
-
Permalink:
muzaffersenkal/langchain-ucp@5cd561c4761d661b275f633313bcaeb3881a07ec -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/muzaffersenkal
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5cd561c4761d661b275f633313bcaeb3881a07ec -
Trigger Event:
release
-
Statement type:
File details
Details for the file langchain_ucp-0.2.0-py3-none-any.whl.
File metadata
- Download URL: langchain_ucp-0.2.0-py3-none-any.whl
- Upload date:
- Size: 33.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
32d7501073e1a90c2966ec995bd8c9e0511f2f8a0d51c21d22d6f09b3a701e2b
|
|
| MD5 |
c1a8dcf2f8b7d99b93d8abfb3dc346f4
|
|
| BLAKE2b-256 |
1ec0260695103ba91753f9e7283de0b61e15f725394dd53b6a1d7b663117fdd8
|
Provenance
The following attestation bundles were made for langchain_ucp-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on muzaffersenkal/langchain-ucp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
langchain_ucp-0.2.0-py3-none-any.whl -
Subject digest:
32d7501073e1a90c2966ec995bd8c9e0511f2f8a0d51c21d22d6f09b3a701e2b - Sigstore transparency entry: 912971026
- Sigstore integration time:
-
Permalink:
muzaffersenkal/langchain-ucp@5cd561c4761d661b275f633313bcaeb3881a07ec -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/muzaffersenkal
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5cd561c4761d661b275f633313bcaeb3881a07ec -
Trigger Event:
release
-
Statement type: