Compiling Solidity
The compile_solidity tool compiles one or more Solidity source files with the Remix compiler. It returns the ABI, bytecode, method identifiers, gas estimates, and metadata for every contract it finds.
Price: 0.01 USDC per call.
Input parameters
{
sources: {
[filename: string]: {
content: string
}
},
version?: string,
settings?: {
optimizer?: {
enabled: boolean,
runs: number
},
evmVersion?: string
}
}
Parameter |
Type |
Required |
Description |
|---|---|---|---|
|
Object |
Yes |
Map of filename to source code content |
|
string |
Yes |
Solidity source code |
|
string |
No |
Solidity compiler version, for example |
|
Object |
No |
Compiler settings |
|
Object |
No |
Optimizer configuration |
|
boolean |
No |
Enable the optimizer. Defaults to |
|
number |
No |
Optimizer runs. Defaults to |
|
string |
No |
EVM version. Defaults to |
Compiler settings
The settings.evmVersion field accepts any of the following:
homesteadtangerineWhistlespuriousDragonbyzantiumconstantinoplepetersburgistanbulberlinlondonparisshanghaiosaka(default)
Turning the optimizer on reduces the gas cost of the deployed contract, so it is worth enabling for anything you intend to put on a live network.
Output
{
success: boolean,
contracts?: {
[filename: string]: {
[contractName: string]: {
abi: Array<Object>,
evm: {
bytecode: {
object: string,
opcodes: string,
sourceMap: string,
linkReferences: Object
},
deployedBytecode: {
object: string,
opcodes: string,
sourceMap: string,
linkReferences: Object
},
methodIdentifiers: Object,
gasEstimates: Object
},
metadata: string
}
}
},
sources?: {
[filename: string]: {
id: number,
ast: Object
}
},
errors?: Array<{
severity: "error" | "warning",
message: string,
formattedMessage: string,
sourceLocation?: {
file: string,
start: number,
end: number
}
}>,
settings?: {
optimizer: {
enabled: boolean,
runs: number
},
evmVersion: string
},
version?: string
}
Output fields
Field |
Type |
Description |
|---|---|---|
|
boolean |
Whether compilation succeeded |
|
Object |
Compiled contracts by file and contract name. Only present on success |
|
Array |
Contract ABI |
|
Object |
Contract creation bytecode |
|
Object |
Runtime bytecode |
|
Object |
Function signature hashes |
|
Object |
Gas cost estimates |
|
string |
Contract metadata JSON |
|
Object |
Source file information with AST. Only present on success |
|
Array |
Compilation errors and warnings, if there are any |
|
Object |
Compiler settings used. Only present on success |
|
string |
Solidity compiler version used |
Example
const result = await client.callTool({
name: "compile_solidity",
arguments: {
sources: {
"MyToken.sol": {
content: `
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract MyToken {
string public name = "MyToken";
mapping(address => uint256) public balances;
function mint(address to, uint256 amount) public {
balances[to] += amount;
}
}
`
}
},
version: "v0.8.20+commit.a1b79de6", // Optional: specify compiler version
settings: {
optimizer: {
enabled: true,
runs: 200
},
evmVersion: "paris"
}
}
});
const output = JSON.parse(result.content[0].text);
console.log('Compiler version:', output.version);
console.log('ABI:', output.contracts['MyToken.sol'].MyToken.abi);
console.log('Bytecode:', output.contracts['MyToken.sol'].MyToken.evm.bytecode.object);
Errors
When the source does not compile, success is false and the errors array describes what went wrong. Each entry carries a severity of either error or warning, so warnings can appear alongside a successful compilation.
{
"errors": [
{
"severity": "error",
"message": "ParserError: Expected ';' but got 'identifier'",
"formattedMessage": "ParserError: Expected ';' but got 'identifier'\n --> MyContract.sol:5:9:\n |\n5 | uint x\n | ^",
"sourceLocation": {
"file": "MyContract.sol",
"start": 78,
"end": 79
}
}
]
}