ai-agent-book 精选快照(<2MB 代码与文档,来自 github.com/bojieli/ai-agent-book)
Build latest book artifacts / build (push) Canceled after 0s
dependency resolution / resolve (3.11) (push) Canceled after 0s
dependency resolution / resolve (3.13) (push) Canceled after 0s
deploy-pages / build (push) Canceled after 0s
deploy-pages / deploy (push) Canceled after 0s
i18n consistency check / check (push) Canceled after 0s
provider adoption tests / test (chapter2/context-compression) (push) Canceled after 0s
provider adoption tests / test (chapter2/prompt-injection) (push) Canceled after 0s
provider adoption tests / test (chapter2/system-hint) (push) Canceled after 0s
provider adoption tests / test (chapter3/log-sanitization) (push) Canceled after 0s
web-search-agent tests / test (push) Canceled after 0s
web-search-agent tests / agentbook (push) Canceled after 0s
Build latest book artifacts / build (push) Canceled after 0s
dependency resolution / resolve (3.11) (push) Canceled after 0s
dependency resolution / resolve (3.13) (push) Canceled after 0s
deploy-pages / build (push) Canceled after 0s
deploy-pages / deploy (push) Canceled after 0s
i18n consistency check / check (push) Canceled after 0s
provider adoption tests / test (chapter2/context-compression) (push) Canceled after 0s
provider adoption tests / test (chapter2/prompt-injection) (push) Canceled after 0s
provider adoption tests / test (chapter2/system-hint) (push) Canceled after 0s
provider adoption tests / test (chapter3/log-sanitization) (push) Canceled after 0s
web-search-agent tests / test (push) Canceled after 0s
web-search-agent tests / agentbook (push) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
# MCP Examples
|
||||
|
||||
This directory contains examples and demos for using MCP (Model Context Protocol) tools and servers within the AWorld framework.
|
||||
These examples showcase how to build, extend, and interact with various MCP-enabled services, including virtual file systems, calculators, media processing, and more.
|
||||
|
||||
## Already cases
|
||||
|
||||
- **BFCL/**
|
||||
Demonstrates Basic Function Call Learning (BFCL) using a virtual file system (GorillaFileSystem) and MCP tools.
|
||||
- Shows how to synthesize function call samples for model training.
|
||||
- Includes a virtual file system agent, MCP tool implementations, and trajectory collection for training data.
|
||||
- See `BFCL/README.md` for detailed instructions and architecture diagrams.
|
||||
|
||||
- **mcp_demo/**
|
||||
Provides a minimal MCP server and client demo pipeline.
|
||||
- Includes a simple calculator server and example pipeline.
|
||||
- Shows how to start an MCP server, configure LLM API keys, and run a sample agent pipeline.
|
||||
- See `mcp_demo/README.md` for step-by-step usage.
|
||||
|
||||
- **mcp_servers/**
|
||||
A collection of ready-to-use MCP servers for various tasks, such as:
|
||||
- Search (text, image, video, document)
|
||||
- Reasoning and calculation
|
||||
- Audio and browser automation
|
||||
- Downloading and file management
|
||||
- Each server is implemented as a standalone Python module.
|
||||
- Useful for extending agent capabilities with external tools.
|
||||
|
||||
- **text_to_audio/**
|
||||
Example of an MCP server and agent for text-to-audio conversion.
|
||||
- Includes a sample MCP server and configuration for audio synthesis tasks.
|
||||
|
||||
## Usage
|
||||
|
||||
- Each subdirectory contains its own entry point (usually `run.py`) and may include additional configuration or requirements files.
|
||||
- Before running any example, ensure you have installed all required dependencies and set the necessary environment variables (e.g., LLM provider credentials, API keys).
|
||||
- For detailed instructions, refer to the README or comments within each subdirectory.
|
||||
|
||||
## Typical Scenarios
|
||||
|
||||
- **Function Call Synthesis:**
|
||||
Generate training data for LLMs by collecting agent trajectories and function call samples (see BFCL).
|
||||
|
||||
- **Custom MCP Servers:**
|
||||
Extend agent capabilities by running your own MCP servers for search, reasoning, media, or file operations.
|
||||
|
||||
- **Pipeline Demos:**
|
||||
Quickly test agent-server interaction with the provided demo pipelines.
|
||||
|
||||
---
|
||||
|
||||
If you need more detailed usage instructions or want to add new MCP tools, refer to the documentation and code samples in each subdirectory.
|
||||
@@ -0,0 +1,2 @@
|
||||
# coding: utf-8
|
||||
# Copyright (c) 2025 inclusionAI.
|
||||
@@ -0,0 +1,30 @@
|
||||
import mcp
|
||||
from mcp.client.streamable_http import streamablehttp_client
|
||||
import json
|
||||
import base64
|
||||
|
||||
config = {
|
||||
"githubPersonalAccessToken": "token"
|
||||
}
|
||||
# Encode config in base64
|
||||
config_b64 = base64.b64encode(json.dumps(config).encode()).decode()
|
||||
api_key = "fkey"
|
||||
|
||||
# Create server URL
|
||||
url = f"https://url?config={config_b64}&api_key={api_key}"
|
||||
print(url)
|
||||
|
||||
async def main():
|
||||
# Connect to the server using HTTP client
|
||||
async with streamablehttp_client(url) as (read_stream, write_stream, _):
|
||||
async with mcp.ClientSession(read_stream, write_stream) as session:
|
||||
# Initialize the connection
|
||||
await session.initialize()
|
||||
# List available tools
|
||||
tools_result = await session.list_tools()
|
||||
print(f"Available tools: {', '.join([t.name for t in tools_result.tools])}")
|
||||
result = await session.call_tool("search_code", {
|
||||
"per_page":10,
|
||||
"q": "bubble sort language:python"
|
||||
})
|
||||
print(result)
|
||||
@@ -0,0 +1,41 @@
|
||||
import random
|
||||
|
||||
import requests
|
||||
from mcp.server.fastmcp import FastMCP
|
||||
from pydantic import Field
|
||||
|
||||
# Create server
|
||||
mcp = FastMCP("streamable-server")
|
||||
|
||||
|
||||
@mcp.tool(description="Perform addition operation")
|
||||
def add(a: int=Field(
|
||||
description="First number",
|
||||
), b: int=Field(
|
||||
description="Second number",
|
||||
)) -> int:
|
||||
"""Add two numbers"""
|
||||
print(f"[debug-server] add({a}, {b})")
|
||||
return a + b
|
||||
|
||||
|
||||
# @mcp.tool()
|
||||
# def get_secret_word() -> str:
|
||||
# print("[debug-server] get_secret_word()")
|
||||
# return random.choice(["apple", "banana", "cherry"])
|
||||
|
||||
|
||||
@mcp.tool(description="Get weather for a city")
|
||||
def get_current_weather(city: str=Field(
|
||||
description="City name"
|
||||
)) -> str:
|
||||
print(f"[debug-server] get_current_weather({city})")
|
||||
|
||||
endpoint = "https://wttr.in"
|
||||
response = requests.get(f"{endpoint}/{city}")
|
||||
return response.text
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# mcp.run(transport="streamable-http")
|
||||
pass
|
||||
Reference in New Issue
Block a user