-
Notifications
You must be signed in to change notification settings - Fork 491
Messari action provider #549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
stat
merged 10 commits into
coinbase:messari-action-provider
from
Bijan-Massoumi:messari-action-provider
Mar 27, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
48cbf02
add action provider
Bijan-Massoumi d740468
Add Messari action provider with tests
Bijan-Massoumi 4895e4c
revert chatbot.ts example
Bijan-Massoumi 9366104
add changelog
Bijan-Massoumi 0590513
Add README for Messari action provider
Bijan-Massoumi c92aeb7
clean up error handling
Bijan-Massoumi 2c35705
update tests
Bijan-Massoumi 16d86f4
update docs with rate limit
Bijan-Massoumi d7c84d9
update the prompt
Bijan-Massoumi ee95ec4
make the description shorter
Bijan-Massoumi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@coinbase/agentkit": patch | ||
--- | ||
|
||
Add a new Messari action provider that enables AI agents to query the Messari AI toolkit for crypto market research data. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
typescript/agentkit/src/action-providers/messari/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# Messari Action Provider | ||
|
||
The Messari Action Provider enables AI agents to query the [Messari AI toolkit](https://messari.io/) for crypto market research data. This provider allows agents to ask research questions about market data, statistics, rankings, historical trends, and information about specific protocols, tokens, or platforms. | ||
|
||
## Getting an API Key | ||
|
||
To use the Messari Action Provider, you need to obtain a Messari API key by following these steps: | ||
|
||
1. Sign up for a Messari account at [messari.io](https://messari.io/) | ||
2. After signing up, navigate to [messari.io/account/api](https://messari.io/account/api) | ||
3. Generate your API key from the account dashboard | ||
|
||
For more detailed information about authentication, refer to the [Messari API Authentication documentation](https://docs.messari.io/reference/authentication). | ||
|
||
Different subscription tiers provide different levels of access to the API. See the [Rate Limiting](#rate-limiting) section for details. | ||
|
||
## Configuration | ||
|
||
Once you have your Messari API key, you can configure the provider in two ways: | ||
|
||
### 1. Environment Variable | ||
|
||
Set the `MESSARI_API_KEY` environment variable: | ||
|
||
```bash | ||
MESSARI_API_KEY=your_messari_api_key | ||
``` | ||
|
||
### 2. Direct Configuration | ||
|
||
Pass the API key directly when initializing the provider: | ||
|
||
```typescript | ||
import { messariActionProvider } from "@coinbase/agentkit"; | ||
|
||
const provider = messariActionProvider({ | ||
apiKey: "your_messari_api_key", | ||
}); | ||
``` | ||
|
||
## Rate Limiting | ||
|
||
The Messari API has rate limits based on your subscription tier: | ||
|
||
| Subscription Tier | Daily Request Limit | | ||
|-------------------|---------------------| | ||
| Free (Unpaid) | 2 requests per day | | ||
| Lite | 10 requests per day | | ||
| Pro | 20 requests per day | | ||
| Enterprise | 50 requests per day | | ||
|
||
If you need more than 50 requests per day, you can contact Messari's sales team to discuss a custom credit allocation system for your specific needs. | ||
|
||
## Actions | ||
|
||
### `research_question` | ||
|
||
This action allows the agent to query the Messari AI toolkit with a research question about crypto markets, protocols, or tokens. | ||
|
||
#### Input Schema | ||
|
||
| Parameter | Type | Description | | ||
|-----------|--------|--------------------------------------------------------------| | ||
| question | string | The research question about crypto markets, protocols, or tokens | | ||
|
||
#### Example Usage | ||
|
||
```typescript | ||
import { AgentKit, messariActionProvider } from "@coinbase/agentkit"; | ||
import { getLangChainTools } from "@coinbase/agentkit-langchain"; | ||
import { createReactAgent } from "@langchain/langgraph/prebuilt"; | ||
import { ChatOpenAI } from "@langchain/openai"; | ||
|
||
// Initialize AgentKit with the Messari action provider | ||
const agentkit = await AgentKit.from({ | ||
actionProviders: [messariActionProvider()], | ||
}); | ||
|
||
// Get LangChain tools from AgentKit | ||
const tools = await getLangChainTools(agentkit); | ||
|
||
// Create a LangChain agent with the tools | ||
const llm = new ChatOpenAI({ model: "gpt-4o-mini" }); | ||
const agent = createReactAgent({ | ||
llm, | ||
tools, | ||
}); | ||
|
||
// The agent can now use the Messari research_question action | ||
// Example prompt: "What is the current price of Ethereum?" | ||
``` | ||
|
||
#### Example Response | ||
|
||
``` | ||
Messari Research Results: | ||
|
||
Ethereum (ETH) has shown strong performance over the past month with a 15% price increase. The current price is approximately $3,500, up from $3,000 at the beginning of the month. Trading volume has also increased by 20% in the same period. | ||
``` | ||
|
||
## Network Support | ||
|
||
The Messari Action Provider is network-agnostic, meaning it supports all networks. The research capabilities are not tied to any specific blockchain network. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./messariActionProvider"; | ||
export * from "./schemas"; |
200 changes: 200 additions & 0 deletions
200
typescript/agentkit/src/action-providers/messari/messariActionProvider.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
import { messariActionProvider, MessariActionProvider } from "./messariActionProvider"; | ||
|
||
const MOCK_API_KEY = "messari-test-key"; | ||
|
||
// Sample response for the research question action | ||
const MOCK_RESEARCH_RESPONSE = { | ||
data: { | ||
messages: [ | ||
{ | ||
role: "assistant", | ||
content: | ||
"Ethereum (ETH) has shown strong performance over the past month with a 15% price increase. The current price is approximately $3,500, up from $3,000 at the beginning of the month. Trading volume has also increased by 20% in the same period.", | ||
}, | ||
], | ||
}, | ||
}; | ||
|
||
// Sample error response in Messari format | ||
const MOCK_ERROR_RESPONSE = { | ||
error: "Internal server error, please try again. If the problem persists, please contact support", | ||
data: null, | ||
}; | ||
|
||
describe("MessariActionProvider", () => { | ||
let provider: MessariActionProvider; | ||
|
||
beforeEach(() => { | ||
process.env.MESSARI_API_KEY = MOCK_API_KEY; | ||
provider = messariActionProvider({ apiKey: MOCK_API_KEY }); | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
delete process.env.MESSARI_API_KEY; | ||
}); | ||
|
||
describe("constructor", () => { | ||
it("should initialize with API key from constructor", () => { | ||
const customProvider = messariActionProvider({ apiKey: "custom-key" }); | ||
expect(customProvider["apiKey"]).toBe("custom-key"); | ||
}); | ||
|
||
it("should initialize with API key from environment variable", () => { | ||
process.env.MESSARI_API_KEY = "env-key"; | ||
const envProvider = messariActionProvider(); | ||
expect(envProvider["apiKey"]).toBe("env-key"); | ||
}); | ||
|
||
it("should throw error if API key is not provided", () => { | ||
delete process.env.MESSARI_API_KEY; | ||
expect(() => messariActionProvider()).toThrow("MESSARI_API_KEY is not configured."); | ||
}); | ||
}); | ||
|
||
describe("researchQuestion", () => { | ||
it("should successfully fetch research results", async () => { | ||
const fetchMock = jest.spyOn(global, "fetch").mockResolvedValue({ | ||
ok: true, | ||
json: async () => MOCK_RESEARCH_RESPONSE, | ||
} as Response); | ||
|
||
const question = "What is the current price of Ethereum?"; | ||
const response = await provider.researchQuestion({ question }); | ||
|
||
// Verify the API was called with the correct parameters | ||
expect(fetchMock).toHaveBeenCalled(); | ||
const [url, options] = fetchMock.mock.calls[0]; | ||
|
||
// Check URL | ||
expect(url).toBe("https://api.messari.io/ai/v1/chat/completions"); | ||
|
||
// Check request options | ||
expect(options).toEqual( | ||
expect.objectContaining({ | ||
method: "POST", | ||
headers: expect.objectContaining({ | ||
"Content-Type": "application/json", | ||
"x-messari-api-key": MOCK_API_KEY, | ||
}), | ||
body: JSON.stringify({ | ||
messages: [ | ||
{ | ||
role: "user", | ||
content: question, | ||
}, | ||
], | ||
}), | ||
}), | ||
); | ||
|
||
// Check response formatting | ||
expect(response).toContain("Messari Research Results:"); | ||
expect(response).toContain(MOCK_RESEARCH_RESPONSE.data.messages[0].content); | ||
}); | ||
|
||
it("should handle non-ok response with structured error format", async () => { | ||
const errorResponseText = JSON.stringify(MOCK_ERROR_RESPONSE); | ||
|
||
jest.spyOn(global, "fetch").mockResolvedValue({ | ||
ok: false, | ||
status: 500, | ||
statusText: "Internal Server Error", | ||
text: async () => errorResponseText, | ||
} as Response); | ||
|
||
const response = await provider.researchQuestion({ | ||
question: "What is the current price of Bitcoin?", | ||
}); | ||
|
||
// Should use the structured error message from the response | ||
expect(response).toContain("Messari API Error: Internal server error"); | ||
expect(response).not.toContain("500"); // Should not include technical details when we have a structured error | ||
}); | ||
|
||
it("should handle non-ok response with non-JSON error format", async () => { | ||
const plainTextError = "Rate limit exceeded"; | ||
|
||
jest.spyOn(global, "fetch").mockResolvedValue({ | ||
ok: false, | ||
status: 429, | ||
statusText: "Too Many Requests", | ||
text: async () => plainTextError, | ||
} as Response); | ||
|
||
const response = await provider.researchQuestion({ | ||
question: "What is the current price of Bitcoin?", | ||
}); | ||
|
||
// Should fall back to detailed error format | ||
expect(response).toContain("Messari API Error:"); | ||
expect(response).toContain("429"); | ||
expect(response).toContain("Too Many Requests"); | ||
expect(response).toContain(plainTextError); | ||
}); | ||
|
||
it("should handle JSON parsing error in successful response", async () => { | ||
jest.spyOn(global, "fetch").mockResolvedValue({ | ||
ok: true, | ||
json: async () => { | ||
throw new Error("Invalid JSON"); | ||
}, | ||
} as unknown as Response); | ||
|
||
const response = await provider.researchQuestion({ | ||
question: "What is the market cap of Solana?", | ||
}); | ||
|
||
expect(response).toContain("Unexpected error: Failed to parse API response"); | ||
expect(response).toContain("Invalid JSON"); | ||
}); | ||
|
||
it("should handle invalid response format", async () => { | ||
jest.spyOn(global, "fetch").mockResolvedValue({ | ||
ok: true, | ||
json: async () => ({ data: { messages: [] } }), // Empty messages array | ||
} as Response); | ||
|
||
const response = await provider.researchQuestion({ | ||
question: "What is the market cap of Solana?", | ||
}); | ||
|
||
expect(response).toContain( | ||
"Unexpected error: Received invalid response format from Messari API", | ||
); | ||
}); | ||
|
||
it("should handle fetch error", async () => { | ||
const error = new Error("Network error"); | ||
jest.spyOn(global, "fetch").mockRejectedValue(error); | ||
|
||
const response = await provider.researchQuestion({ | ||
question: "What is the market cap of Solana?", | ||
}); | ||
|
||
expect(response).toContain("Unexpected error: Network error"); | ||
}); | ||
|
||
it("should handle string error with JSON content", async () => { | ||
// This simulates a case where an error might be stringified JSON | ||
const stringifiedError = JSON.stringify(MOCK_ERROR_RESPONSE); | ||
jest.spyOn(global, "fetch").mockRejectedValue(stringifiedError); | ||
|
||
const response = await provider.researchQuestion({ | ||
question: "What is the market cap of Solana?", | ||
}); | ||
|
||
// Should parse the JSON string and extract the error message | ||
expect(response).toContain("Messari API Error: Internal server error"); | ||
}); | ||
}); | ||
|
||
describe("supportsNetwork", () => { | ||
it("should always return true as research is network-agnostic", () => { | ||
expect(provider.supportsNetwork({ protocolFamily: "evm" })).toBe(true); | ||
expect(provider.supportsNetwork({ protocolFamily: "solana" })).toBe(true); | ||
expect(provider.supportsNetwork({ protocolFamily: "unknown" })).toBe(true); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.