The Model Smart Contract Protocol (MSCP) is a standard protocol that enables LLM applications to interact with EVM-compatible networks.
Project description
Model Smart Contract Protocol (MSCP)
A standard protocol that enables LLM applications to interact with EVM-compatible networks.
Features
Component as a service
AI Agent interacts with the network by operating different components.
Fast integration
Component-based design makes it easier to build workflows and accelerates the development of AI applications.
Unified interaction
Use consistent rules and protocols to standardize the calls to contracts with different functions and ensure the consistency of AI interactions.
Dynamic expansion
AI Agent can add custom onchain components with greater flexibility.
EVM compatibility
It can interact with multiple EVM-compatible network contracts at the same time, and has greater adaptability in handling tasks in complex scenarios.
Decentralization
Access component capabilities without permission, share onchain data, and provide persistent services and information verification.
Architecture
MSCP consists of three parts:
Component: This is a smart contract that complies with ERC-7654.
Connectors: This is used to establish a connection with a component and convert its methods into tool functions that can be interacted with by the LLM.
In addition, it supports customization, you can customize different Connectors according to different smart contracts.
Chat2Web3: This is a router that dynamically calls the connector based on the response from the LLM.
Quick Start
Install
pip install mscp
Set up environment variables
Please refer to .env.example file, and create a .env file with your own settings. You can use two methods to import environment variables.
Deploy Component Smart Contract
Here is a simple component example.sol that you can deploy on any network.
Integrate MSCP into your AI application
from openai import OpenAI
from eth_account import Account
from mscp import Chat2Web3
from mscp.connectors import ERC7654Connector
from dotenv import load_dotenv
import os
load_dotenv()
# Create a connector to connect to the component
component_connector = ERC7654Connector(
"http://localhost:8545", # RPC of the component network
"0x0E2b5cF475D1BAe57C6C41BbDDD3D99ae6Ea59c7", # component address
Account.from_key(os.getenv("EVM_PRIVATE_KEY")),
)
# Create a Chat2Web3 instance
chat2web3 = Chat2Web3([component_connector])
# Create a client for OpenAI
client = OpenAI(api_key=os.getenv("OPENAI_KEY"), base_url=os.getenv("OPENAI_API_BASE"))
# Set up the conversation
messages = [
{
"role": "user",
"content": "What is the user's name and age? 0x8241b5b254e47798E8cD02d13B8eE0C7B5f2a6fA",
}
]
# Add the chat2web3 to the tools
params = {
"model": "gpt-3.5-turbo",
"messages": messages,
"tools": chat2web3.functions,
}
# Start the conversation
response = client.chat.completions.create(**params)
# Get the function message
func_msg = response.choices[0].message
# fliter out chat2web3 function
if func_msg.tool_calls and chat2web3.has(func_msg.tool_calls[0].function.name):
# execute the function from llm
function_result = chat2web3.call(func_msg.tool_calls[0].function)
messages.extend(
[
func_msg,
{
"role": "tool",
"tool_call_id": func_msg.tool_calls[0].id,
"content": function_result,
},
]
)
# Model responds with final answer
response = client.chat.completions.create(model="gpt-3.5-turbo", messages=messages)
print(response.choices[0].message.content)
Integrations
Integrate MSCP into Aser Agent
Aser is a minimalist, modular, and versatile AI agent framework. You can assemble an agent with just a few lines of code. Learn more about aser agent here.
# Create a Connector instance for the component
component_connector = Connector(
"http://127.0.0.1:8545", # RPC URL
"0x0E2b5cF475D1BAe57C6C41BbDDD3D99ae6Ea59c7", # component contract address
Account.from_key(os.getenv("EVM_PRIVATE_KEY")) # Load account from private key in environment variable
)
# Initialize Chat2Web3 with the component connector
chat2web3 = Chat2Web3([component_connector])
# Create an Agent instance with chat2web3
agent = Agent(name="chat2web3", model="gpt-4o", chat2web3=chat2web3)
# Use the agent to chat and get the user's name and age by passing an address
response = agent.chat("What is the user's name and age?0x8241b5b254e47798E8cD02d13B8eE0C7B5f2a6fA")
# Print the response from the agent
print(response)
Integrate smart contracts through Custom Connector
Developers can customize Connector functions to adapt to different contracts. Please refer to the following steps:
1. Depoly Your Contract.
Deploy your custom contract. Here is a CustomConnectorContract.sol as example that you can deploy directly.
2. Implement the Connector.
Developers can implement interface abstraction to customize different connectors
from abc import ABC, abstractmethod
class AbstractConnector(ABC):
def __init__(self, rpc, address, account, type):
self.rpc = rpc
self.address = address
self.account = account
self.type = type
@abstractmethod
def call_function(self, function):
pass
@abstractmethod
def get_functions(self):
pass
You can refer to the custom_connector.
3. Add the Connector to Chat2Web3.
Add the implemented connector to the Chat2Web3 instance.
chat2web3 = Chat2Web3([custom_connector])
You can refer to the custom_connector_example.
Integrate ERC8004 Connector
ERC8004 is a standard interface for smart contracts that enables agents to discover and and establish trust through reputation and validation.
This example demonstrates agent identity registration
from aser import Agent
from mscp import Chat2Web3
from mscp.connectors.erc8004 import ERC8004IdentityConnector
from eth_account import Account
import os
# Load account from environment variable
account = Account.from_key(os.getenv("EVM_PRIVATE_KEY"))
# Initialize ERC8004 identity connector with RPC, contract address, and account
identity_connector = ERC8004IdentityConnector(
"http://127.0.0.1:8545",
"0x8e0E422Ad7BdAbB4e90Edfdd0424039434e38e42",
account
)
# Create Chat2Web3 instance with the identity connector
chat2web3 = Chat2Web3([identity_connector])
# Create an Agent instance, specifying name, model, and chat2web3
agent = Agent(name="chat2web3", model="gpt-4o", chat2web3=chat2web3)
# Send a chat request to create a new agent with domain and address info
response = agent.chat(
f"""
create a newAgent
agentDomain: http://www.ame.network
agentAddress: {account.address}
"""
)
# Print the response from the agent
print(response)
Project details
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 mscp-0.1.3.tar.gz.
File metadata
- Download URL: mscp-0.1.3.tar.gz
- Upload date:
- Size: 13.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e0e7b73fb991b0da4b49ae982ed52a4f62e38a79275d798453cfa698a9c735aa
|
|
| MD5 |
a037e4669715e75c4988898a1fec6660
|
|
| BLAKE2b-256 |
298a520e18b3958d2e119c58314069f89f56725f84f29c3cc6e0a80928294502
|
File details
Details for the file mscp-0.1.3-py3-none-any.whl.
File metadata
- Download URL: mscp-0.1.3-py3-none-any.whl
- Upload date:
- Size: 12.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ec5a4b696dc934feabb570260ff9b093bb21ec4fa4c8d06ea8bdf3398d62a4fd
|
|
| MD5 |
bb60ce4225a3c77d87db39690401de52
|
|
| BLAKE2b-256 |
3308c3a15417d99d43eb6cbb7fcded088de149820d125fa50c873b1d92152c91
|