"""
Example: Basic usage of FastMCP OpenAPI server.
"""
import asyncio
from fastmcp_openapi import OpenAPIServer, AuthConfig
async def main():
"""Example of using the OpenAPI server."""
# Create server
server = OpenAPIServer("Example API Server")
# Add JSONPlaceholder API (a free testing API)
# Note: JSONPlaceholder doesn't provide a proper OpenAPI spec, so commenting out
# await server.add_openapi_spec(
# name="jsonplaceholder",
# spec_url="https://jsonplaceholder.typicode.com/", # This would need an actual OpenAPI spec
# base_url="https://jsonplaceholder.typicode.com"
# )
# Add Petstore API (classic example)
await server.add_openapi_spec(
name="petstore",
spec_url="https://petstore.swagger.io/v2/swagger.json",
base_url="https://petstore.swagger.io/v2" # Provide the base URL
)
# Example with authentication (commented out for demo)
# auth = AuthConfig(
# type="bearer",
# token="your-api-token-here"
# )
# This would add a hypothetical authenticated API
# await server.add_openapi_spec(
# name="authenticated_api",
# spec_url="https://api.example.com/openapi.json",
# auth=auth
# )
# List loaded APIs
apis = server.list_apis()
print("Loaded APIs:")
for api in apis:
print(f" - {api['name']}: {api['title']} v{api['version']} ({api['operations']} operations)")
# Get detailed info about an API
petstore_info = server.get_api_info("petstore")
if petstore_info:
print("\nPetstore API Operations:")
for op in petstore_info["operations"]:
print(f" - {op['method']} {op['path']}: {op['summary']}")
# Run the server
print("\nTo run the MCP server, use the CLI command:")
print("fastmcp-openapi --spec https://petstore.swagger.io/v2/swagger.json --base-url https://petstore.swagger.io/v2 --transport stdio")
print("\nOr use the MCP Inspector:")
print("npx @modelcontextprotocol/inspector fastmcp-openapi --spec https://petstore.swagger.io/v2/swagger.json --base-url https://petstore.swagger.io/v2 --transport stdio")
# For demo purposes, we'll just show that the server is ready
print(f"\nServer '{server.config.name}' is ready with {len(server.apis)} API(s) loaded!")
# Uncomment the line below to actually run the server (will cause asyncio conflict in this context)
# server.run(transport="stdio")
if __name__ == "__main__":
asyncio.run(main())