Mock Webservice¶
The Mock Webservice is a simulated web service that implements the Unified Intent Mediator (UIM) Protocol. It serves as a reference implementation and testing tool for developers working with the protocol.
Overview¶
The Mock Webservice demonstrates how web services can expose intents, issue Policy Adherence Tokens (PATs), and process intent execution requests using the UIM Protocol. It provides a complete implementation of the server-side components of the protocol.
Features¶
- Intent Definition: Defines and exposes intents with their parameters and metadata.
- Policy Definition: Creates and serves policies that AI agents must adhere to.
- Discovery Endpoints: Provides endpoints for AI agents to discover intents.
- PAT Issuance: Issues PATs to AI agents that agree to adhere to policies.
- Intent Execution: Processes intent execution requests from AI agents.
- Simulated Business Logic: Includes simulated business logic for testing intent execution.
- Web Interface: Provides a web interface for monitoring and managing the service.
Architecture¶
The Mock Webservice is built with a modular architecture that separates concerns and promotes maintainability:
+------------------+
| Web Interface |
+------------------+
|
+------------------+
| API Layer |
+------------------+
|
+------------------+ +------------------+ +------------------+
| Discovery Module | | Policy Module | | Execution Module |
+------------------+ +------------------+ +------------------+
| | |
+------------------+ +------------------+ +------------------+
| Intent Registry | | Crypto Service | | Business Logic |
+------------------+ +------------------+ +------------------+
| | |
+------------------+
| Data Storage |
+------------------+
Components¶
- Web Interface: Provides a user interface for monitoring and managing the service.
- API Layer: Handles HTTP requests and responses.
- Discovery Module: Manages intent discovery and service information.
- Policy Module: Handles policy definition, serving, and PAT issuance.
- Execution Module: Processes intent execution requests.
- Intent Registry: Stores and manages intent definitions.
- Crypto Service: Handles cryptographic operations like key management and signature verification.
- Business Logic: Implements the actual functionality behind intents.
- Data Storage: Manages persistent storage of intents, policies, and other data.
Installation¶
Prerequisites¶
- Node.js 14 or higher
- npm (Node.js package manager)
Steps¶
-
Clone the repository:
-
Install dependencies:
-
Generate keys (if not already present):
-
Configure the service (see Configuration section).
-
Start the service:
Configuration¶
The Mock Webservice can be configured using a configuration file or environment variables.
Configuration File¶
Create a file named config.json
in the service's directory:
{
"service": {
"name": "Mock E-commerce Service",
"description": "A mock e-commerce service for testing the UIM Protocol",
"url": "http://localhost:3000",
"logo_url": "http://localhost:3000/logo.png",
"terms_url": "http://localhost:3000/terms",
"privacy_url": "http://localhost:3000/privacy"
},
"server": {
"port": 3000,
"host": "localhost"
},
"keys": {
"private_key_path": "./keys/private-key.pem",
"public_key_path": "./keys/public-key.pem"
},
"policy": {
"rate_limit": 1000,
"rate_limit_period": "hour",
"intent_price": 0.01,
"currency": "USD"
},
"storage": {
"type": "memory"
},
"logging": {
"level": "info",
"file": "./logs/service.log"
}
}
Environment Variables¶
You can also use environment variables to configure the service:
UIM_SERVICE_NAME
: The name of the serviceUIM_SERVICE_DESCRIPTION
: A description of the serviceUIM_SERVICE_URL
: The URL of the serviceUIM_SERVER_PORT
: The port to run the server onUIM_SERVER_HOST
: The host to run the server onUIM_PRIVATE_KEY_PATH
: Path to the private key fileUIM_PUBLIC_KEY_PATH
: Path to the public key fileUIM_RATE_LIMIT
: Rate limit for intent executionUIM_RATE_LIMIT_PERIOD
: Period for rate limiting (second, minute, hour, day)UIM_INTENT_PRICE
: Price per intent executionUIM_CURRENCY
: Currency for intent pricingUIM_STORAGE_TYPE
: Type of storage (memory, file, mongodb)UIM_LOG_LEVEL
: Logging level (debug, info, warn, error)UIM_LOG_FILE
: Path to the log file
Usage¶
Accessing the Web Interface¶
Once the service is running, you can access the web interface at http://localhost:3000
(or the URL you configured).
The web interface provides:
- A dashboard with service statistics
- A list of defined intents
- A log of intent executions
- Configuration options
- PAT management
Defining Intents¶
Intents can be defined in the intents.json
file or through the web interface.
Example Intent Definition¶
{
"intent_uid": "mock-ecommerce:searchProducts:v1",
"intent_name": "SearchProducts",
"description": "Search for products based on criteria",
"input_parameters": [
{
"name": "query",
"type": "string",
"required": true,
"description": "Search query"
},
{
"name": "category",
"type": "string",
"required": false,
"description": "Product category"
},
{
"name": "price_range",
"type": "string",
"required": false,
"description": "Price range (e.g., '10-100')"
},
{
"name": "sort_by",
"type": "string",
"required": false,
"description": "Sort criteria"
}
],
"output_parameters": [
{
"name": "products",
"type": "array",
"description": "List of matching products"
},
{
"name": "total_results",
"type": "integer",
"description": "Total number of matching products"
}
],
"endpoint": "/api/products/search",
"tags": ["e-commerce", "search", "products"]
}
Implementing Business Logic¶
The business logic for intents is implemented in the handlers
directory. Each intent has a corresponding handler file.
Example Handler¶
// handlers/searchProducts.js
module.exports = async function searchProducts(parameters) {
const { query, category, price_range, sort_by } = parameters;
// In a real implementation, this would query a database
// For the mock service, we return simulated data
const products = [
{
id: '123',
name: 'Laptop',
price: 1500,
category: 'electronics',
description: 'A powerful laptop'
},
{
id: '456',
name: 'Smartphone',
price: 800,
category: 'electronics',
description: 'A feature-rich smartphone'
}
];
// Filter by category if provided
let filteredProducts = products;
if (category) {
filteredProducts = filteredProducts.filter(p => p.category === category);
}
// Filter by price range if provided
if (price_range) {
const [min, max] = price_range.split('-').map(Number);
filteredProducts = filteredProducts.filter(p => p.price >= min && p.price <= max);
}
// Sort if sort_by is provided
if (sort_by === 'price_asc') {
filteredProducts.sort((a, b) => a.price - b.price);
} else if (sort_by === 'price_desc') {
filteredProducts.sort((a, b) => b.price - a.price);
}
return {
products: filteredProducts,
total_results: filteredProducts.length
};
};
API Endpoints¶
The Mock Webservice provides the following API endpoints:
Discovery Endpoints¶
GET /agents.json
: Returns the service information and intentsGET /uim/intents/search
: Searches for intents based on query parametersGET /uim/intents/:intent_uid
: Returns details for a specific intent
Policy Endpoints¶
GET /uim-policy.json
: Returns the service's policyPOST /pat/issue
: Issues a PAT to an AI agent
Execution Endpoints¶
POST /uim/execute
: Executes an intent
DNS Configuration¶
To enable DNS-based discovery, you need to configure TXT records for your domain:
uim-agents-file=http://localhost:3000/agents.json
uim-api-discovery=http://localhost:3000/uim/intents/search
uim-policy-file=http://localhost:3000/uim-policy.json
For local testing, you can use a hosts file entry:
And then configure the TXT records in a local DNS server or use a service like dnsmasq.
Extending the Mock Webservice¶
The Mock Webservice is designed to be extensible. Here are some ways you can extend it:
Adding New Intents¶
To add a new intent:
- Define the intent in
intents.json
or through the web interface - Create a handler file in the
handlers
directory - Implement the business logic for the intent
Adding Storage Backends¶
The Mock Webservice supports different storage backends. To add a new one:
- Create a new file in the
storage
directory - Implement the storage interface
- Register the storage backend in
storage/index.js
Adding Authentication Methods¶
To add a new authentication method:
- Create a new file in the
auth
directory - Implement the authentication interface
- Register the authentication method in
auth/index.js
Testing¶
The Mock Webservice includes a comprehensive test suite to ensure its functionality:
For more detailed test output:
To run specific tests:
Deployment¶
The Mock Webservice can be deployed in various environments:
Docker¶
A Dockerfile is provided for containerized deployment:
Docker Compose¶
A docker-compose.yml file is provided for multi-container deployment:
Cloud Deployment¶
Instructions for deploying to various cloud platforms are available in the deployment
directory.
Troubleshooting¶
Common Issues¶
Server Won't Start¶
If the server won't start:
- Check that the port is not already in use
- Verify that the configuration file is valid
- Ensure that the keys are generated correctly
- Check the logs for error messages
PAT Issuance Errors¶
If PAT issuance is failing:
- Check that the agent's signature is valid
- Verify that the agent's public key is in the correct format
- Ensure that the policy is being served correctly
- Check the logs for error messages
Intent Execution Errors¶
If intent execution is failing:
- Check that the PAT is valid and not expired
- Verify that the intent UID is correct
- Ensure that the parameters are valid for the intent
- Check the handler implementation for errors
- Check the logs for error messages
Logging¶
The Mock Webservice logs information to the console and to a log file. To change the log level:
Log files are stored in the logs
directory.
Contributing¶
We welcome contributions to the Mock Webservice! Please see the Contributing Guide for more information.
License¶
The Mock Webservice is licensed under the Apache License 2.0. See the LICENSE file for details.